Binary to Decimal
Cenlasoft
Posts: 265
Hello,
This may be a stupid question, but is there some spin code or function to convert from binary to decimal?
Thanks
Curtis
This may be a stupid question, but is there some spin code or function to convert from binary to decimal?
Thanks
Curtis
Comments
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.
' 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 )
8-Bit Binary to Decimal
16-Bit Binary to Decimal
32-Bit Binary to Decimal
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[16]~~
' 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[16]~~
waitcnt(clkfreq + cnt)
outa[16]~
waitcnt(clkfreq + cnt)
Thanks,
Curtis
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:
...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:
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
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[16]~~
Convert_B_D
repeat
if MyVal == 15
outa[16]~~
waitcnt(clkfreq + cnt)
outa[16]~
waitcnt(clkfreq + cnt)
PUB Convert_B_D | decimal,i,bin,pad
pad := string("00000000")
bin := string("1111")
bytemove(pad+ (strsize(pad)-strsize(bin) ),bin,strsize(bin) )
decimal := 0
repeat i from 0 to strsize(pad)-1
decimal += (byte[pad]-48)*(|<(strsize(pad)-1-i))
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
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
This assumes that the sensor transmits a serial string containing ASCII 0's and 1's. Therefore, you would most likely have a serial interface to the sensor. Is that how the sensor works? What sensor are you using?
Dave
This thread has no project associated with it. It was merely a question about spin. The reason I asked this question is that a student of mine who recently became a Parallax propeller customer came to me and asked if I could solve or research converting a binary number (%1111) to a decimal number (15). I looked as Mike stated in objects such as Parallax Serial Terminal and others, but the code often called other functions and was confusing. I searched the web and the forums and found nothing on point as well. My student told me that he had asked for help on the forums and received basically a rude response. I assured him that over the years my experience on the forums was always great. I then posted my request. Beaus response was great as usual. I had a few more questions and got help. I apologize if I didnt use the proper directive for posting code. I didnt see any help info in doing that as in the older forum. Again, I am sorry for offending anyone, but sometimes protocol is not needed for a question. I hope I can assure my student and Parallax customer that this is just a misunderstanding.
Thank You,
Curtis