Pages: [1]   Go Down
Author Topic: Concatenating 2 HEX bytes  (Read 828 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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)' "
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25731
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use the left shift operator <<, or multiply by 256
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Faraday Member
**
Karma: 58
Posts: 2778
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or, you could just use:

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

http://arduino.cc/en/Reference/WordCast
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: