In my code I need to add b and a into a string. but it is important for me to add them as hex
for example:
b = 2; >>>hex is also 2
a=88; >>> hex is 58
e=288;
I need the hex values, for this issue e should be 258.
how could I add a and b as their hex values in e...
here is my code:
while (Res_Id !=0x1F1)
{
while (!(mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK))
{
}
Res_Id=canMsg.can_id;
Res_Dlc=canMsg.can_dlc;
Res_Data0=canMsg.data[0];
if (Res_Id==0x1F1)
{
for (int i = 2; i<3; i++)
{
Serial.println(i);
a =(canMsg.data[i] & 0xf0)>>4;
Serial.println(a,HEX);
e += a;
//Serial.println(b,HEX);
}
for (int i = 1; i<2; i++)
{
Serial.println(i);
b = canMsg.data[i];
Serial.println(b,HEX);
e += b;
}
Serial.println(e);
e="";
}
}
Res_Id=0;
int i=2;
Did you know that all variables are stored as binary values, so adding them in HEX is a meaningless concept.
Hex and decimal are two ways of looking at a variable's value.
It is only when you want to display a value or send it as a string that you need to specify an interpretation of that number.
No.
That is only part of your code. We need to see all of it, so that it compiles and we can try it ourselves.
For example we can't see the type definition of the variable 'e'. With
and
That will not compile anyway.
A variable in quote marks is for a string variable, and adding string variables to a numeric variable makes no sense at all.
You can save yourself hours and hours of frustration by learning about variables and computer memory as bits.
When you print a value you have choice of number base to show when in memory there are only bits. 258 decimal is 100000010 binary that in decimal is the 256 plus 2 that those 1's are and add up to.
Study and practice should end your confusion, you need more pieces before you can see the picture of this jigsaw puzzle.
Try this code and if it's the result you want, apply the concept in your code.
int a;
int b;
int c;
String e;
void setup() {
Serial.begin(115200);
delay(10);
a = 2;
Serial.println(a, HEX);
c = a << 8;
Serial.println(c, HEX);
b = 88;
Serial.println(b, HEX);
c = c | b;
Serial.println(c, HEX);
e = String(c,HEX);
Serial.println(e);
}
void loop() {
}
Here it was important to use HEX. I couldnt give you hole my code because it needs three Modules integrated with arduino to work...also it needed a parallel software to generate data by CAN protocol.
I explain about my code as it was possible for me.
e should be 288 at the end this string will be empty and in next turn it would get new data generated by that software
In my case I need to read a data by CAN protocol.The data constructed by 8bytes... byte2 and half of byte3 (first4bits) was needed for me. I should put the half of byte3 at first and hole byte2 after it to produce a 12bit. in one example for fisrt four bits was 2 and second 8bits was 58. in an string I produce 258. 2 for first4bits and 58 for 8bits after. so if these 12 bite completed by decimal the final number for me would be 288. so it is different with 258 and they are not the same. so I need to use hex when Im adding the number to the string.
Sorry, but I can make no sense of your example. Perhaps if you provided an example of the 8 bytes that you received and what it is meant to represent it would help
Is the structure of the data in the 8 bytes documented anywhere ?
I got my solution...but if you are interested I can continue... These 8byte data are generated in car ECU and they are on CAN of the ECU by their specific CAN ID. I read one of these CAN massages... all of them are 8bytes... the velocity of the vehicle are stored in byte2 and half of byte3. in this ECU datasheet it says the velocity you need is construced by 12bits...first 4bits are the half of byte3 and 8 remained bits are hole byte2(8bits)
I save half of byte3 as a.
and save hole of byte2 as b.
the hole massage that I'm reading is this:
0B 1B 58 27 3B 4B 5B 6B 7B
here you see this massage contain 8bytes.
I need hole of 58 in this massage(byte2) I stored this in b
and half of 27>>> 2 (first 4bits of byte3) I stored this in a
now I should use a new String for merging a and b.
now
a = 2 as hex and a=2 as decimal
b = 58 as hex and b = 88 as decimal
so when Im adding a and b to string e as decimal it would be 288