Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hexadecimal To Decimal convertion 1

Status
Not open for further replies.

Shantanu

Programmer
Oct 25, 2001
6
0
0
IN
Please Help Me out I want X86 Assembly Snippet to convert Hexadecimal Number To its Decimal Counterpart

Thanx In Advance

Shantanu
 
I presume that the hexadecimal and decimal are in ASCII string format?

The technique here is to convert the hexadecimal string into an integer, and then to convert the integer to a decimal string.

Code:
hexstringtoint proc
;input: si=start of sz string
;output: si=end of sz string, ax=integer
push bx

mov ax,0
mov bx,0
loop1hsti:
;get a byte from the string
mov bl,byte ptr [si]
;check if it's end of string
test bl,0ffh
jz outofloop1hsti
;check if it's an alphabetical character
cmp bl,64
jbe notalpha
;adjust alpha characters
sub bl,7
notalpha:
;determine the value of the digit we just got
and bl,0fh
;shift our accumulator
shl ax,4
add ax,bx
inc si
jmp loop1hsti
outofloop1hsti:
pop bx
ret
hexstringtoint endp

Code:
decimaldata dw 10000,1000,100,10,1
inttodecstring proc
;input: ax=integer to output, si=string space to work in
;output: si=start of decimal string
;NOTE: output string has leading zeros!!!
push di
push bx
push cx
push dx

mov bx,si
mov di,0
loop1itds:
;divide by powers of tens
mov cx,decimaldata[di]
mov dx,0
div cx
;convert result to a digit string
add al,40h
mov byte ptr [di][bx],al
;transfer remainder to ax
mov ax,dx
add di,2
cmp di,10
jb loop1itds

pop dx
pop cx
pop bx
pop di
ret
inttodecstring endp

Anyway those two procedures are off the top of my head, better check through the logic carefully before cutting and pasting it.

Also it is up to YOU to input the data and output it, and to filter it too so that you don't get garbage. He he those are just routines!!

(Seems kind of a lot just to convert hex to dec, eh? Don't busk it, it gets harder when you are making an application.) "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
<bump> &quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
<bump>

Say... maybe I should actually write a FAQ for this? Doing integer to string and string to integer conversions... What d'you-all think?

It's just that I'm just a tad too lazy to write a FAQ... &quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
A faq would be *extremely* helpful, both for myself and I would presume many others as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top