Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert hex string to byte array in CAPL?

Considering having, for example, this type of hex string:

char hex_str[100] = "0x01 0x03 0x04 0x0A";

How to get out of this string the byte array representation in CAPL, like:

byte hex_str_as_byte_arr[4] = {0x01, 0x03, 0x04, 0x0A};

EDIT: Only Vector CANoe supported data types/functions are allowed!

like image 594
PythonNoob Avatar asked Oct 26 '25 20:10

PythonNoob


2 Answers

Thanks to all... Actually I've found a solution myself:

  char hex_str[100] = "0x01 0x03 0x04 0x0A";
  long data[4];
  dword pos = 0;

  pos = strtol(hex_str, pos, data[0]);
  pos = strtol(hex_str, pos, data[1]);
  pos = strtol(hex_str, pos, data[2]);
  pos = strtol(hex_str, pos, data[3]);

  write("0x%02x,0x%02x,0x%02x, 0x%02x", data[0], data[1], data[2], data[3]);

Now it's a simple cast: (byte) data[0]

like image 129
PythonNoob Avatar answered Oct 28 '25 09:10

PythonNoob


Use strtok to split the character array into separate hex strings, then use long strtol( const char *restrict str, char **restrict str_end, int base ) to convert each hex string to an integral value.

like image 34
Stephan Lechner Avatar answered Oct 28 '25 10:10

Stephan Lechner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!