I have a problem when trying to convert a 6 digits number into three hexa decimal numbers I'm using an Arduino Nano I already did the following
- Converting to string Hexa
- Cutting string with substring into three other strings But my CAN library want to receive char variables So i did convert the strings to char but it lack the "0x00" and use instead just "00" And my can library (mcp_can) doesn't want that
Thanks you for reading this
- Tried sending the strings without the "0x" suffix : didn't work
- Tried to add the "0x" suffix to the char : didn't work
- Tried to send decimal : didn't work
- If I send manually something in hexa with the "0x" suffix it work
Edit : The code, i think it's horrible
void splitAndConvert(int bignumber)
{
int ID6;
int ID7;
int ID8;
String FirstPart;
String SecondPart;
String ThirdPart;
String BigNumberHexa = String(bignumber, HEX);
if (BigNumberHexa.length() <= 2) {
FirstPart = "0";
SecondPart = "0";
ThirdPart = BigNumberHexa;
}
else if (BigNumberHexa.length() < 4) {
FirstPart = "0";
SecondPart = BigNumberHexa.substring(0,BigNumberHexa.length() - 2);
ThirdPart = BigNumberHexa.substring(BigNumberHexa.length() - 2,BigNumberHexa.length());
}
else if (BigNumberHexa.length() < 6) {
FirstPart = BigNumberHexa.substring(0,BigNumberHexa.length()-4);
SecondPart = BigNumberHexa.substring(BigNumberHexa.length() - 4,BigNumberHexa.length() - 2);
ThirdPart = BigNumberHexa.substring(BigNumberHexa.length() - 2,BigNumberHexa.length());
}
else {
}
ID6 = ThirdPart.toInt();
ID7 = SecondPart.toInt();
ID8 = ThirdPart.toInt();
unsigned char DataToSend[8] = {0x00, 0x00, 0x00, 0x00, 0x00, ID6, ID7, ID8};
can.sendMsgBuf(0x001, 0, 8, DataToSend);
}