News:

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

Converting from Hex to ASCII and back again.

Started by Lightman, May 20, 2009, 10:15:25 AM

Previous topic - Next topic

Lightman

Hi Everybody,

Can any of you guys help me out with a little hex problem.  Using an example from this web site, I created a program that displayed hex values in ASCII form... eg.. '05B3C2'

I have got that far.. I now have a string of ASCII... '05 B3 C2'. I am trying to convert that back to hex values again.  I got my hex to ASCII converter from this site and tried to reverse the code, but I am getting nowwhere.

Can anyone give me a pointer as to where to start? I have included my code so you can see how far I have got...

Regards,

Lightman



[attachment deleted by admin]

Tedd

Start by working out how to convert just a single digit.
Then you can extend that to two digits by realising the 'high' digit represents 16 times more.
And you can extend that to any number of digits, since each successive digit is 16 times more than the previous (in decimal, each digit is 10 times more.)
No snowflake in an avalanche feels responsible.

BATSoftware

Here is a code snippet that will convert ASCIZ(ie ASCII with a terminating NULL) or ASCII of a known length into a 32-bit value returned in EDX. ESI is passed the starting address of the ASCII buffer, CX contains -1 for ASCIZ or the actual length for ASCII. The function will return with the C-flag set if a non-HEX ASCII character is encountered with ESI pointing to the bad character or C-flag cleared and ESI pointing to the next byte if the conversion encountered no errors:

CVT_ASCHEX PROC
   XOR   EDX,EDX
   JCXZ   @@10   ;CX=0, EMPTY STRING
;
@@1:   MOV   AL,[ESI]   ;GET 1 BYTE
   SUB   AL,'0'
   JB   @@10   ;B=INVALID CHARACTER
;
   CMP   AL,9
   JBE   @F   ;BE=VALID HEX DIGIT
;
   SUB   AL,7
   JS   @@10   ;S=INVALID CHARACTER
;
   CMP   AL,15
   JA   @@10
;
@@:   SHL   EDX,4
        OR      DL,AL           ;STORE DIGIT
   INC   ESI
   LOOP   @@1      ;LOOP THROUGH ALL CHARACTERS
;
@@10:   RET
CVT_ASCHEX ENDP

Farabi

Read it each four bit, make a look up table for it.


LUT db "123456789ABCDEF",0

.code

lea eax,LUT

mov ecx,value
and ecx,0fh

mov dl,[eax+ecx]
mov byte ptr[buffer],dl


Something like that.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

dedndave

there ya go - only put a 0 in there, too

LUT db "0123456789ABCDEF"