我想对可执行文件进行编码和解码问题是关于NULL文件内容为MZ(NULL),因此二进制代码为01001101 01011010 00000000输出应为:4d (for M -> 0100 1101)5a(for Z -> 0101 1010) and00(for NULL -> 0000 0000)以十六进制表示:346435610000但它是:346435613930这是我的代码: CHAR* WriteBuffer = NULL;BYTE *ReadBuffer = NULL;DWORD fSize = 0;OVERLAPPED ol = {0};BYTE AsciiTable[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};int a = 0;//...ReadFile(hndlRead, *ReadBuffer, *fSize, NULL, &ol);for(DWORD i=0; i< fSize; i++){ a = ReadBuffer[i]& 0xf0; if (a > 15) { a = a / 16; } wsprintfA(WriteBuffer, "%s%x", WriteBuffer, AsciiTable[a]); a = ReadBuffer[i] & 0x0f; wsprintfA(WriteBuffer, "%s%x", WriteBuffer, AsciiTable[a]);}编辑/解码:我希望使用AsciiTable进行解码,但是我不能以这种形式进行解码://....int a = 0;for(DWORD i=0; i<dwFileSize; i++){ sscanf_s((CHAR*)ReadBuffer + i*2 ,"%02x" , &a); wsprintfA(szWriteBuffer, "%s%c", szWriteBuffer, a);} 最佳答案 我假设您分配了足够的缓冲区并在写入WriteBuffer之前将其清除。性能问题:您的算法为O(n ^ 2),最好使用此算法,为O(n):CHAR AsciiTable[] = '0123456789ABCDEF';DWORD j=0;for(DWORD i=0; i< fSize; i++){ WriteBuffer[j++]=AsciiTable[ReadBuffer[i]>>4]; WriteBuffer[j++]=AsciiTable[ReadBuffer[i]&0x0f];}更新抱歉,我误会了你的问题。您想将NULL值(字节值0)解码为正好2个连续的0值。在这种情况下,请尝试以下操作:DWORD j=0;for(DWORD i=0; i< fSize; i++){ if (ReadBuffer[i]) { WriteBuffer[j++]=AsciiTable[ReadBuffer[i]>>4]; WriteBuffer[j++]=AsciiTable[ReadBuffer[i]&0x0f]; } else { WriteBuffer[j++]=WriteBuffer[j++]=0; }}更新二:解码器代码如所要求的,这是O(n)中的解码代码,而不是所写的O(n ^ 2)版本。#define ASCII2BYTE(V) (((V)>'9')?(V)-'A':(V-'9')for (DWORD i=0;i<dwFileSize;i+=2){ szWriteBuffer[i>>1]=(ASCII2BYTE(ReadBuffer[i])<<4)+ASCII2BYTE(ReadBuffer[i+1]);}更新III:ASCII2BYTE宏的工作方式(按要求)要转换4D5A,请尝试使用ReadBuffer =“4D5A”,dwFileSize = 4;执行解码代码。每对字符都被获取,那么第一个是最高有效半字节(= 4位),并且从ASCII转换为十进制('4'-> 4),然后第二个字符'D'被转换为最低有效半字节('D'-> 12)大于(4关于c++ - 编码可执行文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29892211/
10-11 21:58