简体   繁体   English

在C ++或汇编中将原始字节转换为十六进制(FASM)

[英]convert raw bytes to hexadecimal in c++ or assembly(FASM)

I know this sounds like a really dumb question but I am trying to build a simple hexadecimal editor and I can't read the bytes from the file. 我知道这听起来像是一个很愚蠢的问题,但我试图构建一个简单的十六进制编辑器,但无法从文件中读取字节。 I use readfile api to read 8kb buffer and then I was going to convert the 8kb to hexadecimal but it gives me sometimes only 4 bytes in reverse order or sometimes 0 I am not sure what I am doing wrong. 我使用readfile api读取8kb缓冲区,然后将8kb转换为十六进制,但是有时它只能以相反的顺序提供4个字节,有时却给我0个,我不确定自己在做什么错。 how can i get it to where it will give me the full 8kb in hex representation. 我怎样才能将它提供给我完整的8kb十六进制表示形式。 If their is a better way to convert an entire file to hex please let me know. 如果他们是将整个文件转换为十六进制的更好方法,请告诉我。 Im just looking for the fastest way to read an entire file and display it on the screen in hexadecimal representation. 我只是在寻找最快的方式来读取整个文件并以十六进制表示形式在屏幕上显示它。 thanks 谢谢

FASM syntax but could easily be c++ as well FASM语法,但也很可能是c ++

invoke ReadFile, [hFile],filebuffer,8192, BytesWritten, 0 
cinvoke wsprintfA,buffer1,"%X",[filebuffer]
invoke MessageBoxA,NULL,buffer1,title,MB_ICONINFORMATION+MB_OK 

all data are dd? 所有数据都是dd?

Update I marked the second answer as the answe because I found masm syntax assembly that looked like this, and it was really fast, but I went ahead and went with CryptBinaryToString api http://msdn.microsoft.com/en-us/library/windows/desktop/aa379887 更新我将第二个答案标记为answe,因为我发现了类似这样的masm语法程序集,而且确实非常快,但是我继续使用CryptBinaryToString api http://msdn.microsoft.com/zh-cn/library /窗/桌面/ aa379887

here is fasm syntax assembly code but again could easily become c++ 这是fasm语法汇编代码,但很容易再次成为c ++

invoke ReadFile, [hFile],filebuffer,500, BytesWritten, 0
        push    dwBuffLen
        push    pszHexBuffer
        push    0x0000000b  ; CRYPT_STRING_HEXASCIIADDR
        push    500
        push    filebuffer
        call    [CryptBinaryToStringA]
;pszHexBuffer contains the data

data sections
filebuffer rb 500
BUF_LEN       = 4000
        pszHexBuffer  db BUF_LEN   dup(0)  ; will contain converted bytes in hex. Twice the size of the original buffer at least (Read documentation about the last _In_Out parameter)
        dwBuffLen     dd BUF_LEN

sprintf("%X") will only convert a single integer for you, and even that will be done according to little endian (hence the reversing) and without leading zeroes (hence the sometimes shorter output). sprintf("%X")只会为您转换一个整数,即使按照小端顺序(因此取反),也没有前导零(因此有时会缩短输出)来完成。 You will want a loop and print each byte using %02x format. 您将需要循环并使用%02x格式打印每个字节。

Something a bit more do it yourself style, but this should do the trick. 您可以自己做一些事情,但这应该可以解决问题。

inline char nibble_decode(char nibble)
{
    const char byte_map[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    return byte_map[nibble];
}

char *hex_string(char *buffer, unsigned char *bytes, int length)
{
    for(int i = 0; i < length; i++){
        buffer[i*3] = nibble_decode(bytes[i] >> 4);
        buffer[i*3+1] = nibble_decode(bytes[i] & 0x0f);
        buffer[i*3+2] = ' ';
    }
    buffer[length*3] = '\0';
    return buffer;
}

Typed this from my phone since I'm on a road trip right now, so I am unable to test this snippet. 由于我目前正在旅途中,因此从手机中输入了此代码,因此我无法测试此代码段。 Hopefully it conveys the concept though. 希望它传达了这个概念。

In summary it loops through length number of bytes and will use a bit shift for the upper bits and an and operation for the lower bits. 总而言之,它循环遍历字节的长度数,并且将对高位使用位移,对低位使用and操作。 I also added a space but that should be easy to remove if you don't want it. 我还添加了一个空格,但是如果您不想要它,应该可以轻松删除。
Note: You must preallocate 3*length+1 bytes to buffer. 注意:您必须预先分配3 * length + 1个字节来缓冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM