user2264535
user2264535

Reputation: 17

Arduino Binary to decimal conversion

I am using Arduino to read a 80 bit serial sync binary code, using a digital bin for incoming data and one for timing as latch for when to read a digital data pin. I have converted the input data to TTL and stored into a fixed length (100) array . I have parsed this out even further into 5 different arrays each with fixed binary length. array one is 17 bitts, two is 20, three is 14, four is 18 and five is 11. My question is how to convert each one of these 5 arrays into decimal values that can be then sent to a display?

Upvotes: 0

Views: 14347

Answers (1)

MAZux
MAZux

Reputation: 971

Notice: we are working with bytes(as blocks) when we are writing or reading form serial interfaces.
And I'll discuss one array in my answer.
For your first array with 17 bits:

float num = 0;
int tempNUM = 0;
int i;
for(i = 0; i < 17; i++){
    num = (pow(2, i) * array1[i]) + num;
}
for(i = 1; i < 7; i++){
    tempNUM = num - (pow(10,i) * int(num/pow(10,i))) - tempNUM;
    Serial.print(byte(tempNUM/pow(10,i-1)), 'DEC');
}

array1[17]: it is your first array.
num: has the decimal value of your binary array; num = 2^i * array1[i] + num
While i refers to indexes.
I used tempNUM to temporary store every digit of num value and print it as single byte in decimal.
Examble:
If num = 13 then ->
tempNUM = 13 - (10 * int(1.3)) - 0 = 13 - (10 * 1) = 3
Print (3/1) = 3 as digit and then
tempNUM = 13 - (100 * int(0.13)) - 3 = 13 - (100 * 0) - 3 = 10
Print (10/10) = 1 and so on ...

Upvotes: 2

Related Questions