phuz
1
I am trying to take the decimal ID provided by CAN.getCanId() and put it in hex.
I am doing:
char buffer[1];
itoa (CAN.getCanId(),buffer,16);
but I only get the last 4 digits of the hex ID instead of all 8 (29-bit ID)
I'm guessing iota can only handle 16 bits of an input integer?
phuz
2
Problem solved.
I come from vb.net so I'm sure this isn't the most efficient way, but it works!
char shortID[4];
String completeID;
int canIDhigh = (CAN.getCanId() & 0xFFFF0000) >> 16;
int canIDlow = CAN.getCanId() & 0x0000FFFF;
itoa (canIDhigh,shortID,16);
completeID = shortID;
itoa (canIDlow,shortID,16);
completeID = completeID + shortID;
Serial.println(completeID); //Voila!
Robin2
3
What about Serial.println(number, HEX);
...R
phuz
4
Actually, while that's great and easy, it only works for outputting to the serial port. I need to use this code internally.
system
5
char buffer[1];
itoa (CAN.getCanId(),buffer,16);
The itoa() function adds a NULL terminator. Leaving room in the array for that, how much room is there for your data?
phuz
6
Even if I did a length of 33, it would only do the 4 hex chars. Making size 1, you would think it would truncate it, but it didn't.
phuz
7
OK, for building the data string, I did this:
datastring = datastring + String(buf*,HEX);*
system
8
Even if I did a length of 33, it would only do the 4 hex chars. Making size 1, you would think it would truncate it, but it didn't.
I would NOT think that. What, EXACTLY, does CAN.getCanId() return?
phuz
9
It returns the ID of the transmitted CAN packet in decimal form.
By doing this, I get the fully concatenated hex version of the ID, which is what I want:
long canID = CAN.getCanId();
int canIDhigh = (canID & 0XFFFF0000) >> 16;
int canIDlow = canID & 0X0000FFFF;
itoa(canIDhigh,shortID,16);
completeID = shortID;
itoa(canIDlow,shortID,16);
completeID += shortID;
system
10
It returns the ID of the transmitted CAN packet in decimal form.
Nonsense. If the function returns a long, or unsigned long, you be absolutely certain that the data is in BINARY.
And, you can be sure that ltoa() (long to ASCII) can convert the value to a string in one step.
phuz
11
PaulS:
Nonsense. If the function returns a long, or unsigned long, you be absolutely certain that the data is in BINARY.
And, you can be sure that ltoa() (long to ASCII) can convert the value to a string in one step.
it comes back as a unsigned long.
system
12
phuz:
it comes back as a unsigned long.
What does? If you are referring to the value from CAN.getCanId(), why are you then storing it in a long?