Hi, fellas! I was wondering how to convert a hex number or dword value to ascii character? Is there any ready to use solution or I've got to dig it myself?



/siddhartha
Posted on 2004-11-09 06:51:57 by siddhartha
I'm new to asm, but I'll chip in how I can. In asm you can use sprintf (I think), to output a value from a buffer, and you can specify whether this prints it out in hex or decimal, whether you can specify if you can output the buffer in ascii is something I don't know.
Posted on 2004-11-09 08:41:50 by Nilrem2
its somethin ' like this in fasm



org 100h

mov ax,-$1234 ; Number: -1253
mov bx,16 ; Base: 16 (hexadecimal)
call putsint ; Print it! (signed integer; base: 10 (default))
call newline

mov ax,$ABCD ; Number: ABCDh (hex value)
mov bx,16 ; Base: 16 (hexadecimal)
call putsint ; Print it! (unsigned integer)
call newline

mov ax,-$5433 ; Number: ABCDh (hex value)
mov bx,16 ; Base: 16 (hexadecimal)
call putint ; Print it! (unsigned integer)
call newline

mov ax,-111000111000111b ; Number: 4360
mov bx,2 ; Base: 10 (decimal)
call bwriteword ; Print it! (unsigned integer)
call newline

mov ax,-111000111000111b ; Number: 4360
mov bx,2 ; Base: 10 (decimal)
call bwriteint ; Print it! (unsigned integer)

xor ax,ax ; Function: Wait for key...
int 16h

mov ax,4C00h ; Exit to DOS
int 21h

; Procedures

; Go to next line
newline:
push ax dx
mov ah,02h
mov dl,0Dh
int 21h ; Output CR to screen
mov dl,0Ah
int 21h ; Output LF to screen
pop dx ax
ret

; Print a signed integer to screen
; bx = base (default = 10), ax = number
putsint:
push dx
or ax,ax
jns .printit ; if it's not negative, we don't print any signs
push ax
mov ah,02h
mov dl,"-"
int 21h ; output the "-"
pop ax
neg ax ; do the number positive
.printit:
pop dx
jmp putint ; now we can print the number...
ret

; Print an unsigned integer to screen
; bx = base (default = 10), ax = number
putint:
push ax bx cx dx
cmp bx,2 ; base can't be less than 2 :)
jge .start
mov bx,10 ; using bx = 10 instead
.start:
xor cx,cx ; cx = 0
.new:
xor dx,dx ; dx = 0
div bx ; number / base
push dx ; push the remainder
inc cx ; increase the "digit-count"
cmp ax,0 ; if the quotient still is not 0, do it once more
jnz .new
.loop:
pop dx ; pop the remainder
add dl,30h ; convert the number to a character
cmp dl,"9"
jng .ok ; if the charater is greater than "9" then we have
add dl,7 ; to add 7 to get A as 10, B as 11, and so on...
.ok:
mov ah,02h ; output the character
int 21h
loop .loop
pop dx cx bx ax
ret


bwriteint: ; AX = signed number, BX = base
push dx
or ax,ax
jns .printit ; if it's not negative, we don't print any signs
push ax bx
mov bx,15 ;mov bl,15 ;xor bh,bh
mov ax,$e2d
int 10h
pop bx ax
neg ax ; do the number positive
.printit:
pop dx
jmp bwriteword ; now we can print the number...
ret

bwriteword: ; AX = number, BX = base
push ax bx cx dx
cmp bx,2 ; base can't be less than 2 :)
jge .start
mov bx,10 ; using bx = 10 instead
.start:
xor cx,cx ; cx = 0
.new:
xor dx,dx ; dx = 0
div bx ; number / base
push dx ; push the remainder
inc cx ; increase the "digit-count"
or ax,ax ; if the quotient still is not 0, do it once more
jnz .new
.loop:
pop ax ; pop the remainder
cmp al,10
sbb al,69h
das
push bx
mov bx,15 ;mov bl,15 ;xor bh,bh
mov ah,$e
int 10h
pop bx
loop .loop
pop dx cx bx ax
ret
Posted on 2004-11-09 09:12:34 by >Matrix<
Open up masm32.inc. There is a bunch of functions commented as 'numeric and text conversions'. Use whatever fits you need.
Or search the forum. There has been pretty huge amount of topics concerning your problem.
Posted on 2004-11-09 09:26:35 by arafel
Arafel is right about masm32.inc Hutch's masm32.lib offers a function to convert HEX values to ASCII


htodw : Convert hex string to DWORD
Posted on 2004-11-09 10:17:19 by Vortex
Arafel is right about masm32.inc Hutch's masm32.lib offers a function to convert HEX values to ASCII


htodw : Convert hex string to DWORD



Yeah, thanks, but that's just not what I want. It outputs a dword from hex. I want to feed a function with some number and put it's corresponding ascii character in a buffer, so I can just display it in MessageBox or whatever. I'll keep diggin'. Thanks for the suggestions! And, >Matrix<, thanks for the code! I'll check this out!



/siddhartha
Posted on 2004-11-09 13:09:15 by siddhartha
The topic is covered in one of the "Go" tutorials which can be viewed at www.GoDevTool.com.

Here is the link to the actual file concerned:-

http://www.jorgon.freeserve.co.uk/TestbugHelp/Writinghex.htm

This file actually discusses the relative speed of various methods of converting hex to ascii, as demonstrated in the Testbug demo program.
Posted on 2004-11-09 13:22:09 by jorgon
siddhartha -

The function you're looking for in masm32.inc is either dwtoah or dwtohex. They will convert a DWORD value into an ASCII string, hex format. You should then be able to pass the string to "MessageBox or whatever." ;)
Posted on 2004-11-09 14:48:16 by sirchess
Hi, fellas! I was wondering how to convert a hex number or dword value to ascii character? Is there any ready to use solution or I've got to dig it myself?
/siddhartha


not sure if you want actual dword2str conversion, but here is such one i have done in the past, code is pretty straight-forward.

;void dword2str(IN val:DWORD, OUT buf:DWORD)

dword2str:
pushad
push 10
pop ecx
mov eax,[esp+8*4+8]
mov edi,[esp+8*4+4]
sub esp,64
mov esi,esp
push ecx
pop ebx
add esi,ecx
push esi
@@convert:
sub edx,edx
div ebx
add dl,'0'
mov byte ptr [esi],dl
test eax,eax
jz @@next
dec esi
inc edi
loop @@convert
@@next:
dec esi
mov byte ptr [esi],al
pop esi
std ;reverse
@@copy:
lodsb
stosb
test al,al
jnz @@copy
add esp,64
popad
cld
ret 2*4


or - alternatively -

dword2hexstr:

pushad
mov eax,[esp+8*4+4]
mov edi,[esp+8*4+8]
mov cl,8
@@rnd1:
rol eax,4
push eax
and al,00001111b
add al,30h
cmp al,39h
jbe @@rnd2
add al,07h
@@rnd2:
stosb
pop eax
loop @@rnd1
popad
ret 2*4
Posted on 2004-11-10 16:43:52 by Drocon