Converting from binary to decimal

Blitz3D Forums/Blitz3D Beginners Area/Converting from binary to decimal

Fry Crayola(Posted 2004) [#1]
Ok, part of my code involves using memory banks.

At one point, I use PeekByte to get a value from the bank. I then use Str and Bin$ in succession to convert this to an 8-bit binary number.

Say, for example, this number is 10011011, stored in the variable a$.

Using Left$(a$,4) I can get the first four binary digits "1001".

How can I then convert this 4 digit binary number to decimal?


SoggyP(Posted 2004) [#2]
Hi Folks,

v$ = "1001"
Print dec(v$)
Print Bin$(9)
Stop

Function Dec(b$)
v=0
While Len(b$)>0
	v = (v Shl 1) + Int(Left(B$,1))
	b$ = Right(b$,Len(b$)-1)
Wend
Return v
End Function


Later,

Jes


Miracle(Posted 2004) [#3]
You may not need to convert the value into a string in the first place; in fact, it would be a lot easier not to.

You can grab the two nybbles in a byte like so:

[box]
lower = number And 15
upper = number Shr 4
[/box]


Fry Crayola(Posted 2004) [#4]
Yes, that's much better. A logical Anding of my stored number, and an 8-bit pattern with the required bits set to 1. This would isolate those bits - a few shifts would then get my required number.

Thanks.


SoggyP(Posted 2004) [#5]
Hi Folks,

I dunno, honestly! I answer the question that someone asks and it turns out they were asking for something else. Bleedin' typical! Still there's a function there if you ever need to convert a binary string.

;0)

Later,

Jes


Fry Crayola(Posted 2004) [#6]
Well, either way is good. It depends on which is faster, really.


WolRon(Posted 2004) [#7]
Well, either way is good.
Huh? Both solutions were for different problems...


Fry Crayola(Posted 2004) [#8]
Were they?

In one, I wanted to get four bits of a byte. In the other, I wanted to get four bits of a byte.

You could use either method to get 5 bits, 3 bits, 2. Whatever takes your fancy.


SoggyP(Posted 2004) [#9]
Hi Folks,

No, mine is good for strings not numbers and vice versa.

Later,

Jes


Fry Crayola(Posted 2004) [#10]
Exactly. Curiously enough, it turns out I'll probably need to use both. Wahey!


SoggyP(Posted 2004) [#11]
Hi Folks,

Ok, I feel warm now ;0)

Later,

Jes


Fry Crayola(Posted 2004) [#12]
Hurray!


eBusiness(Posted 2004) [#13]
I don't think you will need both, Bin is only meant for display use, the shift commands does exactly what you want, and are way faster.


Fry Crayola(Posted 2004) [#14]
I think I might need both because some of the bit sequences I'll be accessing are spread over two or more bytes. I'll be reading in patterns, isolating the required bits and then converting them.

SoggyP's use of Bin$ was only to prove that his routine works, as far as I can make out. His routine will take a string containing a bit pattern and produce the correct output. Using shifts will also work - it depends on the nature of the data that is read. I could well end up needing both - though the faster the better.


eBusiness(Posted 2004) [#15]
If you want to read single bits then use the shl shr combo.

(a shl 30) shr 31 will return the second least significant bit of a.


Andy_A(Posted 2004) [#16]
If you want to read single bits then use the shl shr combo.

(a shl 30) shr 31 will return the second least significant bit of a.


So will "var And 2", and it's the fastest!


eBusiness(Posted 2004) [#17]
Hadn't thought about that one, it will return 2 or 0 instead of 1 or 0, but that can be bitshifted if needed.