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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Help with Hex to Binary conversion

Status
Not open for further replies.

tripharn

Programmer
Sep 4, 2002
5
0
0
AU
I have a hex number stored in AL that I want to convert to binary.

I know that each HEX digit is 4 binary numbers, I guess that I use a loop to determine each digit individually?

But I have no idea where to start... any help would be appreciated, or maybe post a good website that answers these sorts of questions.

Thanks
 
STR2BIN will convert a positive string value to binary at the selected base.

STR2BIN
string is terminated with zero byte
al=base (min=2)
ds:edx=string start
ebx=return value
cf=return set if invalid charactors for base found

used registers restored except ebx & flags
------------------------------------------
This little test program shows STR2BIN with the in-string printed then showing the out-value (remember the first byte is least significant).

.386p
.MODEL SMALL
.STACK 200h
.DATA
key1 db '4142A344=$'
key2 db '4142A344',0h
key3 db '0000$'
.CODE
myproc proc
jmp main

STR2bin proc near
;IN:al=base,ds=selector,edx=offset OUT:ebx=value,cf=error
push eax
push ecx
push edx
push ebx
xor ecx,ecx
mov cl,al
xor eax,eax
STR2bin1: xor ebx,ebx
mov bl,[edx]
cmp bl,0h
je STR2bin0
cmp bl,030h
jl STR2binE
cmp bl,03Ah
jl STR2bin3
sub bl,07h
cmp bl,03Ah
jl STR2binE
cmp bl,03Fh
jg STR2binE
cmp bl,al
jge STR2binE
STR2bin3: sub bl,030h
cmp bl,cl
jg STR2binE
push edx
xor edx,edx
mul ecx
add eax,ebx
pop edx
inc edx
jmp STR2bin1
STR2binE: stc
pop ebx
jmp STR2bin2
STR2bin0: clc
mov ebx,eax
pop edx
STR2bin2: pop edx
pop ecx
pop eax
ret
STR2bin endp

main proc near
mov ax,@data
mov ds,ax
mov ah,9
mov dx,offset key1
int 21h
mov al,0Bh
xor edx,edx
mov dx,offset key2
call STR2bin
jnc main0
mov ebx,' RRE'
main0: mov eax,ebx
xor ebx,ebx
mov bx,offset key3
mov [bx],eax
mov ah,9
xor edx,edx
mov dx,offset key3
int 21h
mov ax,4c00h
int 21h
main endp

myproc endp
end myproc
"People who have nothing to say, say it too loud and have little knowledge. It's the quiet ones you need to worry about!"
 
I'll dig out the BIN2STR if you want. "People who have nothing to say, say it too loud and have little knowledge. It's the quiet ones you need to worry about!"
 
hexadecimal is stored as binary.

values are usually stored using the following methods:
1, binary integers signed or unsigned
2, ascii string
3, BCD or packed BCD
4, single FP, double FP or extended Float point

the most economical for integers is the binary integer. programmers usually use hexadecimal because of the direct corelation to the bits ie 1 hex digit = 4 bits, 1 byte= 2 digits and so on.

numbers are stored in binary allowing the processor to perform calculations quickly and efficiently because no convertions are required.

the only convertion required is during human input or display output where ascii string is required. the input string is converted to binary.

a binary number is base 2. a string can display this binary number in any base usually 2,10 or 16.

the same value can be displayed in many ways:
binary: 01110b
decimal: 014
hex: 0Eh

(i always put leading zero, its my habit probably to make it easier for me to identity hex numbers)
"People who have nothing to say, say it too loud and have little knowledge. It's the quiet ones you need to worry about!"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top