News:

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

ASCII String to Hexadeciamal

Started by Antihaxer, December 30, 2008, 03:14:59 AM

Previous topic - Next topic

Antihaxer

I want to be able to convert say "antihaxer" -> 616E74696861786572h
How would I go about doing this?

jefe


Hi:
      I have made this program. The text must not have more than 100 characters.
       I will send you by e-mail. The mine is dosmil73@hotmail.com

BogdanOntanu

Take care about accepting programs from unknown or untrusted sources ...
There is no program to be made for this.

Once you define an ASCII text it will be already stored as "hex" in your program / memory

For example:

my_string db "ABCDEFGH",0

...it is the very same as:

my_string_hex db 41h, 42h, 43h, 44h, 45h, 46h, 47h, 48h, 00h

But the first form is more easier to be read if the data is in fact an ASCII string.

ASCII, Hexadecimal or Decimal or Binary are different ways to write or note down the very same "physical binary" value.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

Tedd

Take each character in the string, convert its value to hex, print that, repeat.
No snowflake in an avalanche feels responsible.

Antihaxer

Thanks for the replies!  I ended up using this little routine.
; assume 0 <= al <= 0Fh
convert_char proc near
add al, 30h ; 30h <= al <= 3Fh
cmp al, 39h ; is number above '9'?
jbe @ret    ; nope, we're done
add al, 7   ; yes, convert to ascii char
@ret:
ret
convert_char endp

; assume esi = pointer to string
; assume ecx = length of string
; assume edi = output buffer
hex_to_ascii proc near
cld               ; clear direction flag
@loop:
lodsb             ; load a byte from esi, increment it
mov ah, al        ; save the byte
shr al, 4         ; take the top 4 bits
call convert_char ; turn it into an ascii char
stosb             ; store byte at edi, increment it
mov al, ah       
and al, 0fh       ; take the low 4 bits of loaded byte
call convert_char ; turn it into an ascii char
stosb             ; store it at edi, increment it
loop @loop        ; repeat this process #ecx times
ret               ; bye
hex_to_ascii endp

donkey

I wrote a hash function based on the Windows Crypto DLL once and needed it today, it has a routine to convert a byte string to HEX. Since you have shown that you are willing to try to do it yourself I've decided to post it for you, it's not the most efficient of routines but you might find it useful so I adapted it to your needs. You pass a pointer to a string as the target and a pointer to a buffer to hold the resulting string of hex digits for the output, finally you pass the length of the target in cbtarget. I use GoAsm so you will have to translate to MASM on your own.

Translate FRAME ptarget,poutput,cbtarget
LOCAL hextable[4]:D

mov [hextable],"0123"
mov [hextable+4],"4567"
mov [hextable+8],"89ab"
mov [hextable+12],"cdef"

mov ebx,[poutput]
mov edi,[ptarget]
mov ecx,[cbtarget]
xor eax,eax
xor edx,edx
mov esi,offset hextable
:
mov al,[edi]
shr al,4
mov dl,[esi+eax]
mov [ebx],dl
inc ebx
mov al,[edi]
and al,0fh
mov dl,[esi+eax]
mov [ebx],dl
inc edi
inc ebx
dec ecx
jnz <
mov B[ebx],0
RET
ENDF


BTW you can use this on any data, it is not limited to characters or any string, simply pass an array of any byte values and its length and it will translate it to readable hex, as I said I use a similar routine for SHA hashes.

note: when you translate the lines that move quoted text (mov [hextable],"0123") be sure to reverse the text (mov [hextable],"3210") for MASM, GoAsm does this internally.

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable