The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Lightman on May 20, 2009, 10:15:25 AM

Title: Converting from Hex to ASCII and back again.
Post by: Lightman on May 20, 2009, 10:15:25 AM
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]
Title: Re: Converting from Hex to ASCII and back again.
Post by: Tedd on May 20, 2009, 10:33:07 AM
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.)
Title: Re: Converting from Hex to ASCII and back again.
Post by: BATSoftware on May 21, 2009, 03:01:02 PM
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
Title: Re: Converting from Hex to ASCII and back again.
Post by: Farabi on May 22, 2009, 04:08:11 AM
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.
Title: Re: Converting from Hex to ASCII and back again.
Post by: dedndave on May 22, 2009, 04:12:44 AM
there ya go - only put a 0 in there, too

LUT db "0123456789ABCDEF"