Hi everyone.
is there a way to store HEX with the leading zero ? (i need 0x49, not 49 only).
i also need them to be able to be converted to uint8 so i can't do strings.
Any idea ?
the hex value 0x49 will be srored in memory in binary as 01001001
the leading 0x is used
- in code so the compiler knows it is hex value
- in printing so the human reader knows it is a hex value
perhaps give more detail of what you wish to do?
is just the representation for us humans. it has nothing to do how the microcontroller stores the value.
Please explain what you try to solve - because I suspect a xy-problem.
void setup() {
Serial.begin(115200);
uint8_t x = 0x49;
Serial.println(x);
Serial.print("0x"); Serial.println(x, HEX);
Serial.print("0b"); Serial.println(x, BIN);
}
void loop() {
// put your main code here, to run repeatedly:
}
will print:
73
0x49
0b1001001
Print the "0X" first as a string then print the number as hex. Serial.print"47,HEX); That should do it for you.
@horace @noiasca and gilshult (cant tag you sorry new user) Thank you for the reply and knowledge. Actually, it might be an XY problem ![]()
Here it is. So in sending canbus data, here are the code that is needed
#include <FlexCAN.h>
#include <kinetis_flexcan.h>
int led = 13;
// create CAN object
FlexCAN CANReceiver(500000);
static CAN_message_t msg;
static CAN_message_t msg1;
void setup() {
// init CAN bus
Serial.begin(9600);
CANReceiver.begin();
delay(1000);
}
void loop() {
Serial.print("Sending: ");
msg.id = 0x201;
msg.len = 3;
msg.buf[0] = 0x3D;
msg.buf[1] = 0x40; // Register id that i can change to get what type of data i want which is what i want to custom cause i have 6 different register id
msg.buf[2] = 0x00;
Serial.println("");
CANReceiver.write(msg);
delay(500);
while( CANReceiver.read(msg1)) {
Serial.print("Receiving: ");
Serial.println(msg1.id, HEX);
for(int i=0; i<msg1.len; i++) {
Serial.print(msg1.buf[i], HEX); Serial.print(" ");
}
Serial.println("");
}
}
And im trying make a void of sending canbus data (because i have 6 different register id and i want to shorten my code). to be honest i forgot how exactly that ive tried but it goes something like this
#include <FlexCAN.h>
#include <kinetis_flexcan.h>
int led = 13;
// create CAN object
FlexCAN CANReceiver(500000);
unsigned long int exampleRegID = 0x40 ;
static CAN_message_t msg;
static CAN_message_t msg1;
void setup() {
// init CAN bus
Serial.begin(9600);
CANReceiver.begin();
delay(1000);
}
void loop() {
CallCan(exampleRegID);
}
void CallCan (unsigned long regid)
{
Serial.print("Sending: ");
msg.id = 0x201;
msg.len = 3;
msg.buf[0] = 0x3D;
msg.buf[1] = (regid, HEX);
msg.buf[2] = 0x00;
Serial.println("");
CANReceiver.write(msg);
delay(500);
while( CANReceiver.read(msg1)) {
Serial.print("Receiving: ");
Serial.println(msg1.id, HEX);
for(int i=0; i<msg1.len; i++) {
Serial.print(msg1.buf[i], HEX); Serial.print(" ");
}
Serial.println("");
}
}
BUT, ive realize by that, the (regid, HEX) is read 40 instead of 0x40, i thought it should be fine cause they are just the same, but no, the msg.buf[1] need to get exactly 0x40 (or so, because i have tried to send everyway i could possibly think of) . i havent tried to send them in binary or any other typer of data.
I tried to store them in String type of data like
String printhex = "0x" + examplehex;
but no can do cause the msg.buf is saved as and unsigned uint8 and the error says
cannot convert 'String' to 'uint8_t {aka unsigned char}'
i will send more information about the problem once i get my hands on the canbus (on monday), im very sorry if this reply is messy ![]()
Why did you declare this as unsigned long int? That's 32 bits or 4 bytes, but the value you give it is only 1 byte, and it appears like it would always be 1 byte.
What are you attempting to do in this code line? If you think you need to somehow convert regid into hexadecimal, you don't need to do that. It needs to be in binary and it already is in binary. No conversion of any kind is needed. Everything is binary on every digital computer from an Arduino Uno to the world's must powerful supercomputers.
The value "0x3D" here is converted from hex to binary when you upload the code to the Arduino. When the code runs in the Arduino, that value is already in binary, which is what it needs to be.
@bamoloveh
please upload my sketch from #3.
you should see 73, 0x49, 0b1001001 - it is ALL THE SAME.
if you want to SEE the 0x you have to print it.
The controller doesn't care.
The variable will have the value 0b1001001 (or 0x49 or 73).
now read again what @PaulRB has written.
Then try following untested code:
void CallCan (byte regid)
{
msg.id = 0x201;
msg.len = 3;
msg.buf[0] = 0x3D;
msg.buf[1] = regid;
msg.buf[2] = 0x00;
Serial.print("Sending: with buf[1]=");
Serial.print(" 0x");Serial.println(msg1.buf[i], HEX); // print it with a leading 0x
Serial.print(" 0b");Serial.println(msg1.buf[i], BIN); // print it with a leading 0b
Serial.print(" "); Serial.println(msg1.buf[i]); // just in common decimal
Serial.println();
CANReceiver.write(msg);
delay(500);
while( CANReceiver.read(msg1)) {
Serial.print("Receiving: ");
Serial.print(" 0x"); Serial.println(msg1.id, HEX); // print it with a leading 0x
for(int i=0; i<msg1.len; i++) {
Serial.print(" 0x");Serial.print(msg1.buf[i], HEX); // print it with a leading 0x
}
Serial.println("");
}
}
and now you can call the function either as
CallCan (0x40);
CallCan (64);
CallCan (0b01000000);
It is similar.
the "0x" is not part of a binary value. it defines how to interpret the digits in text. 0x means hex (0-9a-f) vs octal (0-7), course binary (0-1) or decimal (0-9)
0x201 means a binary value of 10 0000 0001, not decimal 201 which is 0xC9 or binary 1100 1001 or possibly octal 10 000 001
attempting to store an array of bytes, much less a String object as the value of a single byte variable is clearly not going to work and just confusing you
hopefully you see that the above is correct, the binary value of 0011 1101 is being stored as the first byte of msg
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.