Welcome to the Parallax Discussion Forums, sign-up to participate.

# Binary to Decimal

edited 2011-10-05 - 15:59:22
Hello,
This may be a stupid question, but is there some spin code or function to convert from binary to decimal?
Thanks
Curtis

• edited 2011-10-04 - 10:39:33
All of the display type I/O drivers including FullDuplexSerial have Spin methods called ".dec" that take a number as a parameter and convert it into a sequence of decimal digits. Have a look at any of them.

If you're referring to a conversion from a number to a BCD value (Binary Coded Decimal). That's different and depends on what you actually need. You'll have to say more about that. Usually that's handled with the "/" and "//" operators.
• edited 2011-10-04 - 15:34:45
PRI BCDToNumber(BCD)
' Converts a two-nibble BCD number to a standard binary number

return (((BCD >> 4) * 10) + (BCD & \$F))

PRI NumberToBCD(number)
' Converts a standard binary number to a two-nibble BCD number

return (((number / 10) << 4) + (number // 10))

by Kyle Crane (Based on code by Kwabena W. Agyeman )
• edited 2011-10-04 - 16:53:31
This works, if your binary string is always padded with the appropriate number of zeros.

8-Bit Binary to Decimal
```    decimal := 0
repeat i from 0 to 7
decimal += (byte[string("10101010")][i]-48)*(|<(7-i))
```

16-Bit Binary to Decimal
```    decimal := 0
repeat i from 0 to 15
decimal += (byte[string("0000000010101010")][i]-48)*(|<(15-i))
```

32-Bit Binary to Decimal
```    decimal := 0
repeat i from 0 to 31
decimal += (byte[string("00000000000000000000000010101010")][i]-48)*(|<(31-i))
```
• edited 2011-10-05 - 06:58:21
Thanks everyone,
I think I have got it. One more question for Beau:
I made a function below and I want to recieve a binary number from a sensor and want to pad it for 8 bits with zeros and put it in the place where "00001111" is, a variable.

PUB Main : MyVal | decimal,i
dira~~
' The binary number must be 8 bits and padded with zeros if less than that length
decimal := 0
repeat i from 0 to 7
decimal += (byte[string("00001111")]-48)*(|<(7-i))
repeat
if decimal == 15
outa~~
waitcnt(clkfreq + cnt)
outa~
waitcnt(clkfreq + cnt)
Thanks,
Curtis
• edited 2011-10-05 - 07:36:04
Cenlasoft,

In this example, pad is fixed to the number of binary digits you want (up to 32)
bin contains the binary string you want to convert to decimal

binary to decimal:
```    pad := string("00000000")
bin := string("1111")
decimal := 0
repeat i from 0 to strsize(pad)-1
```

...and to be complete...

In this example, bin is padded and fixed to the number of binary digits you want (up to 32)
decimal contains the decimal value you want to convert to binary

decimal to binary:
```    decimal := 15
bin := string("00000000")
repeat i from 0 to strsize(bin)-1
byte[bin][i] := (((|<(strsize(bin)-1-i))&decimal)>>(strsize(bin)-1-i))+48
```
• edited 2011-10-05 - 08:48:20
Curtis,

I'm a bit confused about what your trying to do. The title of the thread is "Binary to Decimal", but it seems like you want to convert a string of ASCII-encoded binary digits to a number. Is that correct? Are you receiving a string of ASCII-encoded binary digits from the sensor, or is the sensor already providing a binary number? How do you interface to the sensor? Can you post more of your code, or provide more information about the sensor you're using.

Dave
• edited 2011-10-05 - 14:31:53
Thanks again,
The code below is almost where I want it to be:

CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

OBJ

VAR
long MyVal

PUB Main
dira~~
Convert_B_D
repeat
if MyVal == 15
outa~~
waitcnt(clkfreq + cnt)
outa~
waitcnt(clkfreq + cnt)

bin := string("1111")
decimal := 0
repeat i from 0 to strsize(pad)-1
MyVal := decimal

@Beau
I want to use a variable to pass the binary number (bin := string("1111"). I am having problems that it is a string I guess. The value for bin would come from a sensor or me putting it in outside the function.
Thanks,
Curtis
• edited 2011-10-05 - 14:49:50
Curtis,

You have not defined your problem well enough for anybody to provide a solution. Beau's, Clive's and Mike's suggestions are based on their guesses as to what you are trying to do. BTW, when you post code you need use the "code" directive so that the indentation is preserved. The method for doing this is described in the thread at http://forums.parallax.com/showthread.php?129690-How-to-post-program-code-in-the-forum.

Dave
• edited 2011-10-05 - 15:10:24
This is what I think your code looks like with indentation
```VAR
long MyVal

PUB Main
dira~~
Convert_B_D
repeat
if MyVal == 15
outa~~
waitcnt(clkfreq + cnt)
outa~
waitcnt(clkfreq + cnt)

bin := string("1111")
decimal := 0
repeat i from 0 to strsize(pad)-1
MyVal := decimal
```
There are several problems with this. First of all, Convert_B_D is only called once before entering the loop, and then it is never called again. I think you want the call inside of the main repeat loop. You should define pad as an array instead of using the space created by the string value string("00000000"). You could define it in the VAR section as byte pad. You need some interface code that reads the string from the sensor and puts in in the pad array. The code could look like somthing like this:
```VAR
long MyVal

PUB Main
dira~~
repeat
if MyVal == 15
outa~~
waitcnt(clkfreq + cnt)
outa~
waitcnt(clkfreq + cnt)

PUB Convert_B_D(ptr)
repeat while byte[ptr]
result := (result << 1) + (byte[ptr++] & 1)