The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: george999 on November 20, 2007, 08:15:53 PM

Title: bin2hex
Post by: george999 on November 20, 2007, 08:15:53 PM
aa                 db "aa",0
bb                db ?,0
......
invoke bin2hex,addr aa,2,addr bb
invoke SetDlgItemText,hWin,1002,addr bb

In editbox 61 61.

How I can put there like 6161 without spaces?
Title: Re: bin2hex
Post by: MichaelW on November 20, 2007, 10:47:27 PM
The bin2hex procedure formats the hex as it would be formatted for a hex dump, for example:

4D 5A 90 00 03 00 00 00 - 04 00 00 00 FF FF 00 00

Also, your buffer is too small even for the two hex digits and one space from the conversion of a single byte. AFAIK there is no MASM32 procedure or macro that can do exactly what you want, so instead of trying to adapt one of the available procedures or macros I just coded a specialized procedure.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      abc   db "abc",0
      buff  db 10 dup(0)
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

EXTERNDEF hex_table :DWORD

sztohex proc psrc:DWORD,pdest:DWORD

    push esi
    mov esi, psrc
    mov edx, pdest
  @@:
    movzx ecx, BYTE PTR [esi]             ; get byte value from source
    mov al, BYTE PTR [hex_table+ecx*2]    ; get first hex digit from table
    mov [edx], al                         ; copy to destination
    mov al, BYTE PTR [hex_table+ecx*2+1]  ; get second hex digit from table
    mov [edx+1], al                       ; copy to destination
    add edx, 2                            ; adjust pointers
    inc esi
    cmp BYTE PTR [esi], 0                 ; test for end of source
    jnz @B
    mov BYTE PTR [edx], 0                 ; append null to destination

    pop esi
    ret

sztohex endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke sztohex, ADDR abc, ADDR buff
    print ADDR buff,13,10
    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

In case it’s not clear, hex_table contains two characters for each byte value. For example, here are the first and last lines of the table:

db "000102030405060708090A0B0C0D0E0F"
…
db "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"

This is why the byte value from the source string (in ECX) must be scaled by two when it is used to index the table.
Title: Re: bin2hex
Post by: george999 on November 21, 2007, 01:11:37 AM
Thank you verry much.Exactly what i nedd.