News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

ascii to raw hex string (ascii)

Started by lynk, April 02, 2011, 04:58:17 PM

Previous topic - Next topic

lynk


.data
szStringA db 'hello123test',0
szCaption db 'test',0

.data?
szStringB db 120 dup(?)

.code

start

; I don't know how to go about converting szStringA to an ascii string that
; represents the hex values of each character, then storing the result into szStringB.

invoke  MessageBoxA, 0, ADDR szStringB, ADDR szCaption, MB_OK

end start


I'm trying to convert szStringA into another ascii string that is a representation of the hex values of each character from szStringA. e.g.,

hello123test ->
68656c6c6f31323374657374

and storing that result in szStringB.

Any help would be appreciated. Thanks.

dedndave

hex conversion is fairly simple
these routines convert dwords
the order will be little-endian - you may have to play with that

you can write a routine to do it, or use one of the msvcrt functions
        INVOKE  crt__ultoa,dWordValue,offset String,16
the thing i do not like about that function is that it uses lower case letters   :P

i have not tested this routine, but it should work
it will convert a dword (4 bytes at a time)
the output buffer must be at least 9 bytes
HexOut  PROTO   :DWORD,:DWORD

;*************************************************************************************

        OPTION  PROLOGUE:None
        OPTION  EPILOGUE:None

HexOut  PROC    dwValue:DWORD,lpString:DWORD

        push    8
        mov     edx,[esp+12]             ;EDX = lpString
        pop     ecx
        mov     eax,[esp+4]              ;EAX = dwValue
        mov byte ptr [edx+ecx],ch
        dec     edx

HLoop0: push    eax
        and     al,0Fh
        cmp     al,0Ah
        sbb     al,69h
        das                              ;0-9 > '0'-'9', 10-15 > 'A'-'F'
        mov     [edx+ecx],al
        pop     eax
        shr     eax,4
        loop    HLoop0

        ret     8

HexOut  ENDP

        OPTION  PROLOGUE:PrologueDef
        OPTION  EPILOGUE:EpilogueDef

;*************************************************************************************


what you may want is one that does 1 byte at a time
the heart of the routine above is this code
        and     al,0Fh
        cmp     al,0Ah
        sbb     al,69h
        das                              ;0-9 > '0'-'9', 10-15 > 'A'-'F'

it will convert 4 bits into a hex character
do that twice for each byte, using a shift to get the upper nibble first
actually - probably easier to convert the lower nibble first, store it in a register, then do the upper nibble
then store both bytes as a word value

because the bytes you are looking for are reverse the dword order, i think i might use the HexOut routine
but swap the bytes, first
that way, you can convert strings as you like, but also have the code in place to view hex dwords
kind of getting a freebie that way
        mov     edx,offset InString
        mov     ecx,offset OutString

TopOfLoop:
        mov     eax,[edx]  ;get a dword
        push    edx
        bswap   eax        ;reverse the byte order
        push    ecx
        INVOKE  HexOut,eax,ecx
        pop     ecx
        pop     edx
        add     ecx,8      ;point to next out buff
        add     edx,4      ;point to next in dword

you have to watch for the null terminator, but you get the idea

qWord

Quote from: lynk on April 02, 2011, 04:58:17 PMI'm trying to convert szStringA into another ascii string that is a representation of the hex values of each character from szStringA. e.g.,

hello123test ->
68656c6c6f31323374657374
Quote    .data
        align 16
        hex_lut db "0123456789ABCDEF"
        szStringA db 'hello123test',0
    .data?
        szStringB db 120 dup (?)
    .code
   
    lea esi,szStringA
    lea edi,szStringB
    xor ecx,ecx
    .while 1
        movzx eax,BYTE ptr [esi+ecx]
        .break .if !eax
        mov edx,eax
        shr eax,4
        and edx,0fh
        movzx eax,BYTE ptr hex_lut[eax]
        movzx edx,BYTE ptr hex_lut[edx]
        mov BYTE ptr [edi+2*ecx],al
        mov BYTE ptr [edi+2*ecx+1],dl
        lea ecx,[ecx+1]      
    .endw
    mov BYTE ptr [edi+2*ecx],0
    print edi,13,10
FPU in a trice: SmplMath
It's that simple!

dedndave

looks good, qWord   :U
without using an LUT, here is my shot at it   :bg