# How to convert Ascii string (of binary #s) into binary #s??

[b][red]This message was edited by Yevmaster at 2003-12-1 22:47:21[/red][/b][hr]
Hi,
My question is how do I convert ascii string of binary # (input by user) into binary # that I can manipulate with?

I would appreciate any help. Thanks a lot in advance.

Justin

• 1. take the first character from your string
2. subtract the char '0' from it.now you have a 1 digit (as decimal) number
3. check the next char.if its zero,youve done.otherwise you multiply your number by 10 and continue with step 1,taking the next value.

DS:SI should point to your string

xor bx,bx ; bx = ZERO,its your number when youre done
Loop_:
xor ax,ax
lodsb
sub al,'0'
cmp BYTE PTR [si],0
je Done
mov bp,10
mul bp
jmp Loop_
Done:

i havent tested this could,but it should work...
• Hello,
Thanks for responding. Reading your algorithm, I am confused. Also, the execution was incorrect although I might have erred in reprogramming it in 32 bit. My questions for you or anyone who may answer for him are:

1. Aren't you forgetting I am converting into binary not decimal?
2. What does multiplying a 1 or 0 by 10 do and then constantly adding it to bx?

Below is the code I am using for 32 bit.

mov esi, offset binary_file_time
xor ebx, ebx ; ebx is where final value is stored
loop1:
mov al, [esi] ; this and next line can be replaced with lodsb
inc esi
sub al, '0'
cmp byte ptr [esi], 0
je done

mov bp, 10
mul bp

jmp loop1
------------------------------

Justin

: 1. take the first character from your string
: 2. subtract the char '0' from it.now you have a 1 digit (as decimal) number
: 3. check the next char.if its zero,youve done.otherwise you multiply your number by 10 and continue with step 1,taking the next value.
:
: DS:SI should point to your string
:
: xor bx,bx ; bx = ZERO,its your number when youre done
: Loop_:
: xor ax,ax
: lodsb
: sub al,'0'
: cmp BYTE PTR [si],0
: je Done
: mov bp,10
: mul bp
: jmp Loop_
: Done:
:
: i havent tested this could,but it should work...
:

• sorry for confusing you,of coz you asked for a binary number and not a dezimal one

ok,each dezimal number consits of multiple numbers which are multiplied by a power of ten:

123 = 1*10^2 + 2*10^1 + 3*10^0

you have to subtract '0' from the byte holding your char to convert a character to the number:

ASCII BINARY
'5' - '0' = 5

to do this for binary numbers,just muliply with 2 instead of ten (or shift 1 to the left)

here is the code in 32 bit:

mov esi,OFFSET
xor ebx,ebx
Loop1:
lodsb ; char to AL
test al,al ; al == 0 ?
jz Done
shl ebx,1 ; ebx *= 2
sub al,'0'
jmp Loop1

Done:

• : sorry for confusing you,of coz you asked for a binary number and not a dezimal one
:
: ok,each dezimal number consits of multiple numbers which are multiplied by a power of ten:
:
: 123 = 1*10^2 + 2*10^1 + 3*10^0
:
: you have to subtract '0' from the byte holding your char to convert a character to the number:
:
: ASCII BINARY
: '5' - '0' = 5
:
: to do this for binary numbers,just muliply with 2 instead of ten (or shift 1 to the left)
:
: here is the code in 32 bit:
:
: mov esi,OFFSET
: xor ebx,ebx
: Loop1:
: lodsb ; char to AL
: test al,al ; al == 0 ?
: jz Done
: shl ebx,1 ; ebx *= 2
: sub al,'0'

Above gives syntax error. Add al to what?

: jmp Loop1
:
: Done:
:
:
: