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?
Asked
Active
Viewed 1.4k times
0
-
Do you want an algorithm to convert say 1011 to 11? Am I understanding correctly? – Trevor Hutto Jul 29 '14 at 04:37
-
I don't understand anything! What is the sense of the arrays, the different length, what is the info TTL mean here? If you only want to convert binary to ascii please use the functions from the lib. Why we need to know that the data comes from serial io? – Klaus Jul 29 '14 at 09:11
1 Answers
2
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 ...

MAZux
- 911
- 1
- 15
- 28