The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: mnsrl on April 15, 2008, 09:47:42 AM

Title: converting in hex
Post by: mnsrl on April 15, 2008, 09:47:42 AM
I would like to convert the following text:

"My data is Little Endian"

into hexadecimal ASCII codes, using MASM32 library function StdOut. Using the text buffer as an array of DWORD elements..
Title: Re: converting in hex
Post by: ragdog on April 15, 2008, 10:22:12 AM
hi

try this


TextToHex       PROTO :DWORD,:DWORD,:DWORD
b2hex           PROTO :BYTE,:DWORD

.code
TextToHex proc uses edi esi Src: DWORD, Dst: DWORD, Len: DWORD

        ; =====================================
        ; Convert Src binary data to hex in Dst
        ; =====================================

        mov     esi, Src
        mov     edi, Dst

@@:
        invoke  b2hex, [esi], edi
        inc     edi
        inc     edi
        mov     byte ptr[edi], ' '
        inc     edi
        inc     esi
        dec     Len
        jnz     @B

        mov     byte ptr[edi-1], 0 ; Write NULL terminator

        ret
TextToHex endp
b2hex proc source: BYTE, lpBuffer: DWORD

        ; ==========================
        ; Convert source byte to hex
        ; ==========================

        mov     edx, lpBuffer

        xor     eax, eax
        xor     ecx, ecx

        mov     cl, source
        mov     al, cl
        and     al, 00001111b
        cmp     al, 10
        sbb     al, 69h
        das
        mov     [edx+1], al

        mov     al, cl
        shr     al, 4
        cmp     al, 10
        sbb     al, 69h
        das
        mov     [edx], al

        ret
b2hex endp