Combine integers and converting data

Hello,

Currently I am reading in can bus data and I am attempting to work with the following information.

CAN ID: 0x372
0 0 0 0 0 0 3 AC

This data is being passed to " unsigned char buf[8]; " , So if was to run "SERIAL.print(buf[7], HEX);" I would get the value "AC" returned.

I needed to combine these two values and the only way I was able to combine them to be the value "3AC" and not add together as equation was to use the string function.

String byte6 =String(buf[6],HEX);
String byte7 =String(buf[7],HEX);
String afr = String(byte6+byte7);

But now that I have the value of "3AC" I am not sure use 3AC or convert it back to an integer I can read as the decimal value of 940.

I am sure there is a better way to do this and I probably should not be using a String at all, if anyone could give me some insight on where I am going wrong I would greatly appreciate your help.

Thank you

The sum you are looking for is

total = (most significant byte * 256) + least significant byte

Try

unsigned int value = (buf[6]<<8) | buf[7];
Serial.println(value,HEX);

jremington:
Try

unsigned int value = (buf[6]<<8) | buf[7];

Serial.println(value,HEX);

That worked immediately. and made it much simpler.
I tired using bit shifting earlier but must have done something seriously wrong.

Thank you so much.

You need to be really careful with this code; if "buf" is declared as a signed type, like "char", you may not get the results you expect.