Hex value in string to char array/data array

Hello,
I was unable to figure out how to transfer a hex value in string to char array.
I have;

String l = "731c8080"; // these value changes frequently in the loop
String m = "58ed12a5" / /these value changes frequently in the loop
char buf[8];

Now I need like this:
buf[0] = 0x73; // buf[ ] should be updated frequently based on the "l" and "m" values
buf[1] = 0x1c;
buf[2] = 0x80;
buf[3] = 0x80;
buf[4] = 0x58;
buf[5] = 0xed;
buf[6] = 0x12;
buf[7] = 0xa5;

Can i get some solution

I don't recommend using String.
You should use a C-string.

Like this.

char l[] = "731c8080";
char m[] = "58ed12a5";
char buf[8];

void setup() {
  Serial.begin(9600);
  readHex(&buf[0], l); // Write start point, string
  readHex(&buf[4], m);
  for (unsigned int i = 0; i < sizeof(buf); i++) Serial.println((byte)buf[i], HEX);
}

void loop() {
}

void readHex(char* buf, char* txt) {
  char b[3] = "00";
  for (unsigned int i = 0; i < strlen(txt); i += 2) {
    b[0] = *(txt + i);
    b[1] = *(txt + i + 1);
    *(buf + (i >> 1)) = strtoul(b, NULL, 16);
  }
}

Thanks for your reply!
Now how can I call the values of (byte)buf[i], HEX and use for further processing.
like
char hexcon[8];

hexcon[0]= (byte)buf[0], HEX; // So the value of hexcon[0] = 73;
hexcon[1]= (byte)buf[1], HEX; // So the value of hexcon[1] = 1c;
hexcon[2]= (byte)buf[2], HEX; // So the value of hexcon[0] = 80;
hexcon[3]= (byte)buf[3], HEX; // So the value of hexcon[0] = 80;
hexcon[4]= (byte)buf[4], HEX; // So the value of hexcon[0] = 58;
hexcon[5]= (byte)buf[5], HEX; // So the value of hexcon[0] = ed;
hexcon[6]= (byte)buf[6], HEX; // So the value of hexcon[0] = 12;
hexcon[7]= (byte)buf[7], HEX; // So the value of hexcon[0] = a5;

You don't have to, just use it.
I'm just writing it for Serial.println() function.

buf[0]
buf[1]
buf[2]
...

This is exactly what you are looking for.

EDIT:
The value has already been assigned to buf[] as a numerical value.

I would ask how the hex digits get into these strings/Strings to begin with? If they are read from some stream (eg. Serial), then the easiest solution could be to read them directly into your buf[8] array from the stream.

But I want to assign HEX value directly.

  1. I will get the data from GPS on the SerialPrint.
  2. then after applying some conversion formula I will get 10 digit long value. "long lati = 1023456789;"
  3. Now the long values needs to be converted into HEX value (in string format) . "HEX value = 3D00B615"
  4. HEX value needs to be included into an char/data array to transfer as CAN messages.
    buf[0] = 73; // buf should be updated frequently based on the "l" and "m" values
    buf[1] = 1c;
    buf[2] = 80;
    buf[3] = 80;
    buf[4] = 58;
    buf[5] = ed;
    buf[6] = 12;
    buf[7] = a5;
    can message will be sent with following line
    CAN.sendMessage(0x18FFE61C,1,8,buf);

Maybe you guys should team up.

I'm saying already that's the case...
I can't say anything more. :woozy_face:

buf[] is assigned HEX value in above my code!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.