Binary to Decimal

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

Comments

  • Mike GreenMike Green Posts: 22,886
    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.
  • Clive WakehamClive Wakeham Posts: 152
    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 )
  • Beau SchwabeBeau Schwabe Posts: 6,400
    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))
    


    Beau Schwabe -- Submicron Forensic Engineer
    www.Kit-Start.com - bschwabe@Kit-Start.com ෴෴ www.BScircuitDesigns.com - icbeau@bscircuitdesigns.com ෴෴

  • CenlasoftCenlasoft Posts: 265
    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[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
  • Beau SchwabeBeau Schwabe Posts: 6,400
    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")
        bytemove(pad+ (strsize(pad)-strsize(bin) ),bin,strsize(bin) )
        decimal := 0
        repeat i from 0 to strsize(pad)-1
          decimal += (byte[pad][i]-48)*(|<(strsize(pad)-1-i))
    

    ...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
    


    Beau Schwabe -- Submicron Forensic Engineer
    www.Kit-Start.com - bschwabe@Kit-Start.com ෴෴ www.BScircuitDesigns.com - icbeau@bscircuitdesigns.com ෴෴

  • Dave HeinDave Hein Posts: 5,897
    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
  • CenlasoftCenlasoft Posts: 265
    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[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
  • Dave HeinDave Hein Posts: 5,897
    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
  • Dave HeinDave Hein Posts: 5,897
    edited 2011-10-05 - 15:10:24
    This is what I think your code looks like with indentation
    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][i]-48)*(|<(strsize(pad)-1-i))
      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[9]. 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
      byte pad[9]
    
    PUB Main
      dira[16]~~
      repeat
        ReadSensor(@pad)
        MyVal := Convert_B_D(@pad)
        if MyVal == 15
          outa[16]~~
          waitcnt(clkfreq + cnt)
          outa[16]~
          waitcnt(clkfreq + cnt)
    
    PUB Convert_B_D(ptr)
      repeat while byte[ptr]
        result := (result << 1) + (byte[ptr++] & 1)
    
    PUB ReadSensor(ptr)
      ' Put sensor interface code here
    

    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
  • CenlasoftCenlasoft Posts: 265
    edited 2011-10-05 - 15:59:22
    Hello,
    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. Beau’s response was great as usual. I had a few more questions and got help. I apologize if I didn’t use the proper directive for posting code. I didn’t 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
Sign In or Register to comment.