I am trying to accomplish turning a nibble into it's ASCII character and then turn that character into a byte.
In the example below I turn the byte myByte (0x12) into to nibbles. One becomes 0x1 and the other 0x2. What I want to do then is to turn the nibbles into ASCII characters, 1 and 2, and then back to byte to send these over CAN. So ASCII 1 would be hex value 0x31 and ASCII 2 would be 0x32. It does not have to be hex. It could might aswell be decimal 49 and 50.
Is there a way of doing this? I have searched a lot and have not found what I'm looking for. It might be that I'm not really sure what to search for.
Regards,
Christian
byte myByte = {0x12};
byte myData[2];
void setup() {
Serial.begin(115200);
myData[0] = myByte >> 4;
myData[1] = myByte & 0xf;
Serial.println(myData[0]);
Serial.println(myData[1]);
}
void loop() {
// put your main code here, to run repeatedly:
}
A method that CAN uses to communicate with 'dumb' peripherals is with BCD (binary coded decimal) in which every nibble represents a decimal number of 0 thru 9. This appears (heavy emphasis) to be what you're trying to accomplish.
As an example, the number 5555 is binary 0001 0101 1011 0011 and would be transmitted thus. BCD however, would represent 5555 as 0101 0101 0101 0101, stripping each digit into a nibble. CAN is actually an old protocol and the use of BCD was to accommodate 0-9 thumbwheel switches.
Please, let me understand.
What I understond is: you have a char array that contiens a ASCII message. You want take each element, devide it in the octal or exadecimal digits and print than.
IS IT LIKE THIS?
IF IT IS you can cast each element like a int and use the .print function, witch the right argument about base
Then my_union.my_byte = your hex or decimal value, my_union.nibble1 are the four lsb and my_union.nibble2 are the four msb.
The breaking down into two nibbles is already sorted. What I want to to with the nibbles is to convert them into ASCII and then into bytes.
So let's say that one nibble is 0xF. I want to convert this to a byte that contains decimal 70.
Silente:
Please, let me understand.
What I understond is: you have a char array that contiens a ASCII message. You want take each element, devide it in the octal or exadecimal digits and print than.
IS IT LIKE THIS?
IF IT IS you can cast each element like a int and use the .print function, witch the right argument about base
No, you must have misunderstood everything, unless I accidentally added a char array in my example.
I have not understand a thing:
Why have you to transform a value in more than one that conteins exadecimal digits of the same value? The thing can be usefull only in case to show tje number or pass it. But I think that all the .print function has the ability of doing this change of base by thanselves. So why do you need it?
What IS the problem? If the value in the nibble is between 0 and 9, add '0'. If the value is between 10 and 15, subtract 10 and add 'A'.
THAT was the problem. I didn't know how to accomplish the hex or decimal value corresponding to the ASCII characters of the nibbles. Now I do, and for that I thank all you very much!
I'm sorry that it might be somewhat unclear what I want to accomplish. I know my terminology is way off sometimes, but everything I know about Arduino programming is self taught. The only programming classes I have attended are HTML and PHP some 15 years ago.
chrstrvs:
In the example below I turn the byte myByte (0x12) into to nibbles. One becomes 0x1 and the other 0x2. What I want to do then is to turn the nibbles into ASCII characters, 1 and 2, and then back to byte to send these over CAN. So ASCII 1 would be hex value 0x31 and ASCII 2 would be 0x32. It does not have to be hex. It could might aswell be decimal 49 and 50.