Array To Binary, and Convert that from Binary To Decimal

Hi,
I am working a a program that fetches data from an ADC using SPI,
I am able to store the values of the sensor in an array
that looks somewhat like this [0,1,0,1,1,0,1,0]
I have found a binaray to decimal converter
return (((BCD >> 4) * 10) + (BCD & $F)) (https://forums.parallax.com/discussion/134931/binary-to-decimal)
How am i able to convery my array to a proper binary number and then convert that to decimal?
I'm sorry if this question seems dumb i have only experimented with the propeller tool a few days

Comments

  • 14 Comments sorted by Date Added Votes
  • Why do you have an array of ones and zeroes? If you're using SPI, you can build the value(s) on-the-fly.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • @Jon From reading the values of MOSI using pin.in(x)
    Or is it wrong, it's my first time bitbanging
  • A number is a number... the only difference between "binary" and "decimal" is how it's displayed to humans. That is, 127, $7F, and %01111111 are all the same number, displayed in decimal, hex, and binary notations respectively.

    If you've read the bits 0, 1, 0, 1, 1, 0, 1, 0 you probably want to reconstruct the number %01011010. As Jon suggested, it's probably simplest to do this on the fly; something like:
      '' read 8 bits
      val := 0 ' final value
      repeat 8
        bit := next_pin_value ' pseudo code, replace with your real bit reading code
        val := (val << 1) | bit
    
    The final line shifts the contents of val left and then or's in the next bit. At the end you will have an 8 bit value val (i.e. a byte) that you can do whatever you want with.
  • From reading the values of MOSI using pin.in(x)

    I think you probably meant MISO (Master In Slave Out), which is how the ADC (slave device) would return data to the propeller (master device). MOSI (Master Out Slave In) is used for sending data from the propeller (such as commands) to the ADC.

    Walter
    Tulsa, OK

    My OBEX objects:
    AGEL: Another Google Earth Logger
    DHT11 Sensor

    I didn't do it... and I promise not to do it again!
  • @walter yeah i'm sorry, i realised it after writing that comment, it is already switched in my code. but thanks for the comment
  • jespervanbondtjespervanbondt Posts: 5
    edited April 17 Vote Up0Vote Down
    Also if i can add another layer to the question, @ersmith
    https://forums.parallax.com/discussion/134931/binary-to-decimal
    the comment of beau expects a string ... how could i convert the long val to a string so that this code works
    i mean like how do i edit decimal += (byte[string(val)]-48)*(|<(7-i)) so it accepts val as a value
         val := 0 ' final value
         repeat 8
             bit := next_pin_value ' pseudo code, replace with your real bit reading code
             val := (val << 1) | bit
     
         decimal := 0
         repeat i from 0 to 7
             decimal += (byte[string(val)][i]-48)*(|<(7-i))
    
  • Hi,
    I am working a a program that fetches data from an ADC using SPI,
    I am able to store the values of the sensor in an array
    that looks somewhat like this [0,1,0,1,1,0,1,0]
    I have found a binaray to decimal converter
    return (((BCD >> 4) * 10) + (BCD & $F)) (https://forums.parallax.com/discussion/134931/binary-to-decimal)
    How am i able to convery my array to a proper binary number and then convert that to decimal?
    I'm sorry if this question seems dumb i have only experimented with the propeller tool a few days

    I think what your getting at is that your checking the value of the serial input with each clock and storing whether its 1 or 0. Now you want to know how to convert those 1s and 0s into a actual number. If thats what your asking,the answer is that you do it AS you receive them. There are various different ways that people use to receive serial data but they typically do very much the same thing. For example, you could check the value of the bit. Then take the result you get and xor it with a register that you use to accumulate your result. then shift it left. Then get the next bit, xor it, shift left, until you have it all.

    You certainly COULD build your result after the fact. That might be appropriate if for example the data was coming in so fast you had no time to do anything but grab it. Just take a bit, shift left, then xor it,and repeat, until you have built the entire number. It wont be decimal. It will be a binary number (instead of a single bit)
    Particularly patient proactive practice positively predicates practically precise poly-processor Parallax Propeller programming paradigms.

    .
  • Also if i can add another layer to the question, @ersmith
    https://forums.parallax.com/discussion/134931/binary-to-decimal
    the comment of beau expects a string ...
    Yes, because his routine converts a string (like letters "1" "2" "3" "0"), into a binary encoded decimal number (BCD). Note that almost nobody uses BCD encoded numbers anymore, except for very specialized purposes. You probably do *not* want to use that routine, unless you specifically need to communicate with a device that expects BCD. If you read further in the thread you quoted you'll see that the original poster seemed mixed up in his question, so he may have led you astray.

    Sometimes there is some confusion about the difference between a number and a string, since when we type them they look the same, but inside the computer they are very different. The string "123" is stored as four bytes 49, 50, 51, and 0. 49 is the ASCII code for "1" , 50 is the ASCII code for "2", and so on. The *number* 123 can be stored as one byte. When we write that byte in decimal we write it as 123. In hex we would write it as 7B. In binary it would be 01111011. Those are all ways of writing the same number.

    Generally speaking computers store numbers in a kind of binary encoding internally. Some very old computers used binary coded decimal, but it's not really used any more. The Propeller certainly uses binary internally, but generally (for human consumption) most Propeller programs, including Spin, use decimal or hexadecimal notation.

    I guess it would be useful to step back and see exactly what it is you are trying to do with the bits you receive. Is the device sending you ASCII data (a string)? Or is it sending you a value directly? Most devices do the latter, but sometimes the protocol might specify ASCII or some other encoding.

  • @ersmith Thanks for the reply,
    The ADC does not send a string, it just sends the number ( from 0 to 1024 ) in binary i.e 15 = 1111
    This is a link to the docs of my ADC so you have a better understanding of what i'm trying to do https://cdn-shop.adafruit.com/datasheets/MCP3008.pdf
    Page 19 the second area of text is the data receiving, i read the values of MISO and store them into val like you said, now i want to convert that binary value
    to a decimal i.e from 1111 to 15, so i can start storing the sensorvalues. Also the MOSI is already coded and the ADC is configured.
    Also i should note that i don't have the ADC yet, i ordered it and it will arrive tomorrow.
    So could you modify your code to add the conversion from val to decimal?
  • The ADC does not send a string, it just sends the number ( from 0 to 1024 ) in binary i.e 15 = 1111
    This is a link to the docs of my ADC so you have a better understanding of what i'm trying to do https://cdn-shop.adafruit.com/datasheets/MCP3008.pdf
    Page 19 the second area of text is the data receiving, i read the values of MISO and store them into val like you said, now i want to convert that binary value
    to a decimal i.e from 1111 to 15, so i can start storing the sensorvalues. Also the MOSI is already coded and the ADC is configured.
    There's no conversion necessary. The val variable in my code is just a number. It's already "converted" from the incoming binary to the computer's internal representation. All conversion is done. You don't have to do anything else to it -- you can just store it (a := val), test it (if val > 0 dosomething) or do whatever you want with it.
  • The MCP3008 is a 10 bit ADC, so it will be stored in a 16 bit (word) or possibly 32 bit (long) variable. Typically the only other conversion you would want to make is to the ascii numbers that represent that value, so the binary value 1111 would be converted to the two ascii characters "1" and "5". That can be done by a function in FullDuplexSerial and a number conversion object whose name eludes me at the moment.

    There are other conversions that can be done (hex, bcd, octal, etc) for specific uses such as bcd for 7 segment displays.
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • Jespar,
    There is many data type encodings out there. Just a plain number has two very common encodings, namely integer and floating point. Then there is fixed point (close neighbour of integer) and a multitude of string encodings. Integer (fixed point) is usually always native to the processor/hardware.

    The integer (fixed point), as well as being the fastest to handle, is also the most efficient use of RAM, for a linear number at least.

    “What car did you learn to drive stick shift on?” == “What was the make and model of your first car?” -> Password reset! -- https://krebsonsecurity.com/2018/04/dont-give-away-historic-details-about-yourself/
  • evanhevanh Posts: 4,764
    edited April 19 Vote Up0Vote Down
    BTW: I'd class BCD as a string encoding due to it having a memory allocation per decimal digit. However it is somewhat special case due it mostly being a packed encoding. 4-bit calculators will be the exception here.

    “What car did you learn to drive stick shift on?” == “What was the make and model of your first car?” -> Password reset! -- https://krebsonsecurity.com/2018/04/dont-give-away-historic-details-about-yourself/
  • Early mainframe computers had instructions for add/sub/mul/divide on BCD packed values directly in hardware.
    And some used base 16 instead of base 2 for floating point...
Sign In or Register to comment.