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

0

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. how can i get it to where it will give me the full 8kb in hex representation. 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

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?

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

here is fasm syntax assembly code but again could easily become 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
c++
assembly
io
hex
fasm
asked on Stack Overflow May 24, 2014 by Darrin Woolit • edited May 25, 2014 by Jens Björnhager

2 Answers

1

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). You will want a loop and print each byte using %02x format.

answered on Stack Overflow May 24, 2014 by Jester
0

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. 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.

answered on Stack Overflow May 25, 2014 by p4plus2

User contributions licensed under CC BY-SA 3.0