Converting from binary to decimal
Blitz3D Forums/Blitz3D Beginners Area/Converting from binary to decimal
| ||
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? |
| ||
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 |
| ||
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] |
| ||
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. |
| ||
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 |
| ||
Well, either way is good. It depends on which is faster, really. |
| ||
Well, either way is good. Huh? Both solutions were for different problems... |
| ||
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. |
| ||
Hi Folks, No, mine is good for strings not numbers and vice versa. Later, Jes |
| ||
Exactly. Curiously enough, it turns out I'll probably need to use both. Wahey! |
| ||
Hi Folks, Ok, I feel warm now ;0) Later, Jes |
| ||
Hurray! |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
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! |
| ||
Hadn't thought about that one, it will return 2 or 0 instead of 1 or 0, but that can be bitshifted if needed. |