I'm reading a device that gives to my Arduino board a 3 digits HEX number byte by byte in the following way : If the number is 0x128 , I'll receive 3 bytes b1=0x31 , b2=0x32, b3=0x38.
I take without problem the lower nibble of each one (1,2,8) but I don't know how to "mix" all these three numbers in a unique one over one variable to be transformed in the correspondence decimal. Do you have some clue to implement this?
My code is the following :
[/code]
byte b1;
byte b2;
byte b3;
double b4;
int b4dec; // The converted to DEC variable b4
int done;
I tested this
b4 = b1 << 4;
b4 = b2 << 4;
b4 = b3 << 4;
And this
b4 = b1 << 12;
b4 = b2 << 8;
b4 = b3 << 4;
Is it a shift in the number of bits?,
I really need to recover the number 0x128 in HEX and them convert to DEC. Could you give me more indications or do you know a link where I could find documentation for doing this?
Thanks
Step 1: convert each hex byte (character) to its binary value. You have done that, in a way, but I would do it like this:
char msChar; // Most significant Character
char mmChar; // Middle Character
char lsChar; // Least significant Character
byte msB; // Most significant digit value
byte mmB; // Middle digit value
byte lsB; // Least significant digit value
int binValue; // Decimal digits converted to binary
void setup()
{
Serial.begin(19200);
msChar='1';
mmChar='2';
lsChar='3';
Serial.print("Here are the chars: ");
Serial.print(msChar);
Serial.print(" ");
Serial.print(mmChar);
Serial.print(" ");
Serial.println(lsChar);
// convert each char to a (binary) digit value:
msB = msChar - '0';
mmB = mmChar - '0';
lsB = lsChar - '0';
Serial.print("Here are the int values of the digits: ");
Serial.print(msB,HEX);
Serial.print(" ");
Serial.print(mmB,HEX);
Serial.print(" ");
Serial.println(lsB,HEX);
Step 2 combine the binary digit values to a decimal number represented by those digits.
For three digits, it goes like this:
// Method 1: Brute force:
binValue = msB * 100 + mmB * 10 + lsB;
Serial.print("Here is the decimal value of the integer: ");
Serial.println(binValue);
Another way, which has no particular advantage here, but that is more amenable to doing in a loop (where the digits are in an array) is this:
// Method 2: Horners nesting scheme:
binValue = ((msB * 10) + mmB) * 10 + lsB;
Serial.println("Wanna see it again?");
delay(1000);
Serial.print("OK! Here it is again: : ");
Serial.println(binValue);
} // End of the setup() function
void loop()
{
// Does nothing but you can do whatever you want
}
Put them together in a sketch and you will see something like:
Here are the chars: 1 2 3
Here are the int values of the digits: 1 2 3
Here is the decimal value of the integer: 123
Wanna see it again?
OK! Here it is again: : 123
deSilva: I think you are right and my response was non-responsive. I just zeroed in on the part where he said, "transformed in the correspondence decimal," and that's what I did: transformed the chars '1' '2' '3' to 123 decimal, which is definitely not what he actually asked for.
On the other hand, if the problem is actually about hexadecimal, no one actually asked about or addressed the problem of hexadecimal chars. For example, what if the input chars were 'B' '2' 'A' ? Does he want an integer with the value 0xb2a or what?
Regards,
Dave
Footnote:
I fell into the trap of trying to write code without having a program specification. The fact that I wasn't actually paying attention to the original example didn't help either. Oh, well...
Don't feel so pessimistic! You did it well to explain an important aspect of beginner's programming. Be aware that we have here 5 times as much "guests" as active posters. Many of them will profit.