My project reads data from a Can_Bus with the MCP2515 board. I have used other’s libraries to read the messages which are stored in an array/struct. I can read and print the message ID and the data payload, but the ID is a 4byte variable (unsigned long 32) which contains hex info from the full ID. I want to extract the 2nd and 3rd bytes for further processing and ignore the 1st and 4th.
e.g. if the message data is 8DF50B73
I want to extract F50B for further processing.
Can someone advise what functions I can use for this process?
Please be gentle, I am in-experienced with C/C++. My previous coding many years ago was in Basic!
int convertToInt(char* dataFrame, size_t offset, size_t numberBytes) {
// define a local SafeString on the stack for this method
cSFP(frame, dataFrame);
cSF(hexSubString, frame.capacity()); // allow for taking entire frame as a substring
frame.substring(hexSubString, offset, offset + (numberBytes * 2)); // endIdx in exclusive in SafeString V2+
hexSubString.debug(F(" hex number "));
long num = 0;
if (!hexSubString.hexToLong(num)) {
hexSubString.debug(F(" invalid hex number "));
}
return num;
}