News:

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

hex to ascii again

Started by ragdog, November 15, 2009, 07:41:30 PM

Previous topic - Next topic

ragdog

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,

dedndave

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 ?

ragdog

I try to convert a hex string to a ascii string

54006869000073 to T0hi00s


ragdog

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

xandaz

    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'


clive

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]
It could be a random act of randomness. Those happen a lot as well.

xandaz

....
    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)?

dedndave

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

FORTRANS

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.

dedndave

right, Steve
but i think they switch context, meaning they use different stack space

hutch--

Try the two library modules,

hex2bin proc src:DWORD,dst:DWORD
bin2hex proc lpString:DWORD,lnString:DWORD,lpbuffer:DWORD
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

hfheatherfox07

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 ?

dedndave

and another
to convert a binary dword to hex string...
        INVOKE  dw2hex,dwValue,lpStringBuffer

they are all in the Masm32\help\masmlib.chm file

ToutEnMasm

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.

dedndave

yah - but you can call it twice   :bg