Go Down

Topic: Concatenating 2 HEX bytes (Read 1 time) previous topic - next topic

MrCanBus

I really am new to this (C as well as arduino), and I have tried searching but I am still no further forward.  This is part of a CANBUS project using a sparkfun/SKPANG canbus shield, but I'm fairly sure my question is just a general one.

The data that I am extracting from the CANBUS is 8 bytes (8 pairs of HEX characters?).  I can display each byte as HEX or DEC, which is fine for something like engine temp which is contained in a singe byte.  My issue comes with engine RPM which is contained in two seperate bytes.  I've worked out (by analysing the data in EXCEL) that I need to concatinate the bytes as HEX and then convert to DEC.  But I can't work out how to do it.  I've posted a generic example as opposed to the entire code;

Code: [Select]

void setup()
{

  Serial.begin(9600);
 
  byte data1 = 0x1e;
  byte data2 = 0x3d;
  String str12_0x = "0x" + String(data1,HEX)+String(data2,HEX);
 
  Serial.print("data1 HEX = ");
  Serial.println(data1,HEX);

  Serial.print("data2 HEX = ");
  Serial.println(data2,HEX);

  Serial.print("data1 + data2 = ");
  Serial.println(data1+data2);
 
  Serial.print("Str12_0x = ");
  Serial.println(str12_0x);   

  Serial.println(strtol(str12_0x, NULL, 16));

  }

void loop()
{
 
}



I'm just trying to combine data1 1E and data2 3D and then convert the resultant 0x1E3D to an int for printing to a display or file as DEC.  I've managed to combine them (by concatinating two strings) and get a string 0x1E3D but can't for the life of me convert the resultant string to an integer.  I'm sure there is a simple answer to this but I can't find one.  Tried using strtol(str12_0x, NULL, 16) but just get " error: cannot convert 'String' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)' "

AWOL

You could use the left shift operator <<, or multiply by 256
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

MrCanBus

multiple by 256, now there's a simple solution !  I'd got so hell bent on solving the issue I hadn't thought about doing it another way!  Thanks

michinyon

I don't think you need to convert it to an integer.   1e3d  is a 2-byte short integer.    Unless it is initially
coded in binary coded decimal or something.

1e is 30  and 3d is 61   so  1e3d  is  ( 30 x 256 ) + 61   whatever that is.


PeterH

Or, you could just use:

Code: [Select]
int value = word(highByte, lowByte);

http://arduino.cc/en/Reference/WordCast
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Quote
1e3d  is a 2-byte short integer.

Which is the size of an int on the Arduino. The Arduino does not have a short type.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy