I've been searching like crazy for a bin2hex function but couldn't find one anywhere... I wonder if anyone could post one? Maybe even a speed contest :p


It's pretty trivial to convert binary data to hex strings, though. Process byte-by-byte, and process each byte as two nibbles. Use a 16-character "0123456789ABCDEF" lookupstring, and index it with the nibble.


I wonder if someone can also explain to me how to use a lookupstring.
Posted on 2005-10-04 11:19:31 by Lenin
try something like this... coded off top of my head, not tested :)


.data
szHexLookup db "0123456789ABCDEF"

.code

bin2hex:
mov esi,
mov edi,
mov ecx,

.process:
lodsb
mov ah, al
and al, 0xF
movzx edx, al
mov al,
stosb

shr ah, 4
movzx edx, ah
mov al,
stosb

dec ecx
jnz .process

Posted on 2005-10-04 12:01:55 by f0dder
Thanks :) Would you help me with one more thing... I'm coding a notepad, the reason I needed this bin2hex function was so that I could open binary files... I wonder if you could tranform your code into a function.... Something like:
bin2hex proc uses esi, edi sourcedata:DWORD, targetstring:DWORD, sourcelenght:DWORD
mov esi,
mov edi,
mov ecx,

.process:
lodsb
mov ah, al
and al, 0xF
movzx edx, al
mov al,
stosb

shr ah, 4
movzx edx, ah
mov al,
stosb

dec ecx
jnz .process
      ; what would I return?
        ret
bin2hex endp


Would this be correct? I need it to be a function so I can use it here:

LoadTextFileToEdit proc FilePath:DWORD
    LOCAL hFile:HANDLE, dwFileSize:DWORD, pszFileText:LPSTR, dwRead:DWORD

    invoke CreateFile,FilePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL
    .if eax != INVALID_HANDLE_VALUE
        mov hFile, eax
        invoke GetFileSize, hFile, NULL
        .if eax != 0ffffffffh
            mov dwFileSize, eax
            inc eax
            invoke HeapAlloc, ProcessHeap, HEAP_ZERO_MEMORY, eax
            .if eax != NULL
                mov pszFileText, eax
                invoke ReadFile, hFile, pszFileText, dwFileSize, addr dwRead, NULL
                .if eax
                    mov eax,pszFileText
                    add eax,dwFileSize
                    mov byte ptr, 0
                invoke SetWindowText, hwndEdit, pszFileText
               
                invoke GetFileName, FilePath
                invoke wsprintf, addr TitleBuff, addr lpFmt, eax
                invoke SetWindowText, hwnd, addr TitleBuff
                .endif
                invoke HeapFree, ProcessHeap, 0, pszFileText
            .endif
        .endif
        invoke CloseHandle, hFile
    .endif
    xor eax, eax
    ret
LoadTextFileToEdit endp
Posted on 2005-10-04 12:49:58 by Lenin
Assuming that my code is correct :P, your function looks okay (good you remember the register preservation). You don't really need to return anything...
Posted on 2005-10-04 15:56:28 by f0dder
If you work on the binary data one chunk at a time ie one DWORD at a time you can streamline the process by not having to mess with nibbles.
Also using a lookup table of 256 words (if memory isn't an issue would also be faster. 512bytes


B2HLUT db "000102030405060708090A0B0C0D0E0F"
            db "101112131415161718191A1B1C1D1E1F"
            db "202122232425262728292A2B2C2D2E2F"
            db "303132333435363738393A3B3C3D3E3F"
            db "404142434445464748494A4B4C4D4E4F"
            db "505152535455565758595A5B5C5D5E5F"
            db "606162636465666768696A6B6C6D6E6F"
            db "707172737475767778797A7B7C7D7E7F"
            db "808182838485868788898A8B8C8D8E8F"
            db "909192939495969798999A9B9C9D9E9F"
            db "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
            db "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
            db "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
            db "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
            db "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
            db "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"

Bin2Hex:
;IN ptr to array of dwords IE read a file to a buffer or making a filemapping obj
;IN ptr to output string
;IN size of array of dwords, IE number of dwords to make into hex
PUSH EBX
PUSH ESI
PUSH EDI
XOR EBX,EBX
MOV ECX, dword ;len
MOV EDI, dword ; outstr
MOV ESI, dword ;dwords
.LP:
DEC ECX
JS .ALLDONE
MOV EAX, dword
MOV BL, AL
MOV DX, word
SHR EAX,8
MOV word,DX
MOV BL,AL
MOV DX, word
SHR EAX,8
MOV word, DX
MOV BL,AL
MOV DX, word
SHR EAX,8
MOV word,DX
MOV BL, AL
MOV DX, word
MOV word,DX
JMP .LP
.ALLDONE:
POP EDI
POP ESI
POP EBX
RET 12

AlternatveCodeBin2Hex:
;IN ptr to array of binary BYTES
;IN ptr to output string
;IN size of array in BYTES
PUSH EBX
PUSH ESI
XOR EBX,EBX
MOV ECX, dword ;len
MOV EDX, dword ; outstr
MOV ESI, dword ;bytes
DEC ECX
JS .ALLDONE
MOV BL, byte
.LP:
MOV AX, word
MOV BL, byte
MOV word,AX
DEC ECX
JS .ALLDONE
JMP .LP
.ALLDONE:
POP ESI
POP EBX
RET 12


If it works, there's still a lot of room for minor speed optimizations
Posted on 2005-10-04 19:17:03 by r22