The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ragdog on November 15, 2009, 07:41:30 PM

Title: hex to ascii again
Post by: ragdog on November 15, 2009, 07:41:30 PM
hi

I have a problem with this hexdecode routine
Contain the original string with hex bytes 00 can not convert correct hex to ascii

szString db "54006869000073",0


push offset hFormat
push offset szString
call HexDecode

invoke MessageBox,0,offset hFormat,0,MB_OK

HexDecode proc uses esi edi ebx pHexStr:dword,pOutBuffer:dword
;---------------------------------------
   mov    esi, pHexStr
   mov    edi, pOutBuffer
   jmp    @1
@@: and    ebx, 0Fh
   add    eax, ebx
   mov    [edi], al
   inc    edi
@1: movzx  edx, byte ptr[esi]
   cmp    edx, 40h
   sbb    ebx, ebx
   sub    edx, 37h
   and    ebx, 7
   inc    esi
   add    ebx, edx
   js      @F
   mov    eax, ebx
   shl    eax, 4
   mov    [edi], al
   movzx  edx, byte ptr [esi]
   cmp    edx, 40h
   sbb    ebx, ebx
   sub    edx, 37h
   and    ebx, 7
   inc    esi
   add    ebx, edx
   jns    @B
@@: ret
;---------------------------------------
HexDecode endp



Have any an idea?

Greets,
Title: Re: hex to ascii again
Post by: dedndave on November 15, 2009, 09:03:46 PM
i am not sure what you are expecting for output
are you trying to convert it to binary ?
you have a string input and a string output ?
are you trying to convert a decimal string to a hex string ?
Title: Re: hex to ascii again
Post by: ragdog on November 15, 2009, 09:27:32 PM
I try to convert a hex string to a ascii string

54006869000073 to T0hi00s

Title: Re: hex to ascii again
Post by: ragdog on November 15, 2009, 10:05:31 PM
Ok i have solved it

This was my mistake this msgbox can only print The "T"
I look in olly and see in the output buffer the correct format



00403043    54                         
00403044    0068 69                     
00403047    0007                         
00403049    3000                       




Ok thanks
Title: Re: hex to ascii again
Post by: xandaz on October 22, 2011, 12:09:43 PM
    Hi guys... Just the other day i was trying to get my way around HEX(mem) to ASCII and i thought i could post the results. In the past i used to Divide the number by EBX(10H) and the do xlat, but after thinking for a while i got the conclusion that it could be done with ANDs and then xlat. I 'm not sure what spends nore clocks but it gave something like this:

HEXtoASCII PROC Val:BYTE,OutBuff:LPSTR
lea ebx,HexDigits
mov edi,OutBuff
mov al,Val
push ax
and al,11110000b
shr al,4
xlat
stosb
pop ax
and al,1111b
xlat
stosb
xor ax,ax
mov al,'H'
stosw
ret
HEXtoASCII endp


On the other hand convert the number into a binary string can be cone with shifting.


BINtoASCII PROC Val:BYTE,OutBuff:LPSTR
        mov edi,OutBuff 
        mov al,Val
        mov ecx,8
loop_x:
        rcr   al,1
        jnc   _zero
        mov al,'1'

Title: Re: hex to ascii again
Post by: clive on October 22, 2011, 12:35:05 PM
Although that's a hideous mix of 16 and 32-bit code, and XLAT is slow. If this is 32-bit code, don't use PUSH/POP AX

and eax,0Fh
mov eax,HexDigits[eax]
Title: Re: hex to ascii again
Post by: xandaz on October 22, 2011, 07:31:28 PM
....
    stosb
zero:
    mov al,'0'
    stosb
    dec   ecx
jcxz _end
    jmp loop_x
_end:
    xor ax,ax
    mov  al,'b'
    stosw
    ret
BINtoASCII endp

   Why is hbideous clive? About xlat... well i wasn't counting but if you say it's slow you're prolly right... I don't see what the problem is with using 16 bit pushes? Anyone care to explain the ups and downs relative to this specific function (HEXtoASCII)?
Title: Re: hex to ascii again
Post by: dedndave on October 22, 2011, 07:37:18 PM
you should keep the stack 4-aligned in a 32-bit program

it may not be critical in a period when no API calls are made, as the stack is supposed to be private
not really sure about that

at any rate, data transfers in a 32-bit program are always more efficient on 4-aligned addresses

speed may not be an issue for you - i dunno
if it isn't, a 16-byte lookup table still makes for small, efficient code
the 16 bytes cost in data is hard to overcome in code

if speed is an issue, a 512-byte lookup table is probably best
Title: Re: hex to ascii again
Post by: FORTRANS on October 22, 2011, 09:35:53 PM
Quote from: dedndave on October 22, 2011, 07:37:18 PM
you should keep the stack 4-aligned in a 32-bit program

it may not be critical in a period when no API calls are made, as the stack is supposed to be private
not really sure about that

Hi,

   Not sure if it matters, but interrupts still occur and must be serviced.
Stack alignment should not be a factor, but in 32-bit protected mode...

Regards,

Steve N.
Title: Re: hex to ascii again
Post by: dedndave on October 22, 2011, 10:43:43 PM
right, Steve
but i think they switch context, meaning they use different stack space
Title: Re: hex to ascii again
Post by: hutch-- on October 22, 2011, 11:08:54 PM
Try the two library modules,

hex2bin proc src:DWORD,dst:DWORD
bin2hex proc lpString:DWORD,lnString:DWORD,lpbuffer:DWORD
Title: Re: hex to ascii again
Post by: hfheatherfox07 on October 22, 2011, 11:32:52 PM
Quote from: hutch-- on October 22, 2011, 11:08:54 PM
Try the two library modules,

hex2bin proc src:DWORD,dst:DWORD
bin2hex proc lpString:DWORD,lnString:DWORD,lpbuffer:DWORD

Hutch do you have any examples of how to use that anywhere on the forum ?
Title: Re: hex to ascii again
Post by: dedndave on October 22, 2011, 11:47:05 PM
and another
to convert a binary dword to hex string...
        INVOKE  dw2hex,dwValue,lpStringBuffer

they are all in the Masm32\help\masmlib.chm file
Title: Re: hex to ascii again
Post by: ToutEnMasm on October 23, 2011, 07:03:46 AM
Quote
2^32 = 4294967296
2^ ?  = 54006869000073
Your number exceed a 32 bit value.Use a routine who use QWORD value.
dw2hex is limited to  a 32 bit value.
Title: Re: hex to ascii again
Post by: dedndave on October 23, 2011, 10:05:42 AM
yah - but you can call it twice   :bg
Title: Re: hex to ascii again
Post by: ToutEnMasm on October 23, 2011, 03:58:03 PM
Quote
yah - but you can call it twice   
:bdg
you have to made a proc only for that.
Try it.
Title: Re: hex to ascii again
Post by: dedndave on October 23, 2011, 04:04:41 PM
that's the fun part about working with ASCII hex   :P
you don't have to worry about base conversion (i.e. no carry or borrow)
if you want to make a "qw2hex" proc, just be sure to do the high dword first   :U
Title: Re: hex to ascii again
Post by: dedndave on October 23, 2011, 04:10:34 PM
        .XCREF
        .NOLIST
        INCLUDE    \masm32\include\masm32rt.inc
        .LIST

;--------------------------------------------------------------

        .DATA
        ALIGN   4

dqValue dq 0ABCDEF9876543210h

;------------------------------------

        .DATA?
        ALIGN   4

bBuffer db 17 dup(?)

;--------------------------------------------------------------

        .CODE

_main   PROC

        INVOKE  dw2hex,dword ptr dqValue+4,offset bBuffer
        INVOKE  dw2hex,dword ptr dqValue,offset bBuffer+8
        print   offset bBuffer
        print   chr$(13,10)
        inkey
        exit

_main   ENDP

;--------------------------------------------------------------

        END     _main