How to store HEX in a uint8_t array?

3.5k views Asked by At

I want to store the HEX value into a uint8_t array. Below is the code that I am trying to use and pass it to set the resource value:

const static uint8_t PSK_KEY[] = "31383031";
security->set_resource_value(M2MSecurity::Secretkey, PSK_KEY, sizeof(PSK_KEY) - 1);

Or do I need to set the PSK_KEY in ASCII?

2

There are 2 answers

1
unwind On BEST ANSWER

It depends on what you mean.

"Store hex" (why do you write it in caps?) is a bit unclear.

If the value of the PSK is the four bytes 0x31, 0x38, 0x30, 0x31 then you need to write it differently to get the proper result:

static const uint8_t PSK_KEY[] = { 0x31, 0x38, 0x30, 0x31 };

of course those four are ASCII, so you can express the same data as text:

static const uint8_t PSK_KE[] = "1801";
0
Vagish On

You can use

PSK_KEY[]= { 0x31,0x38,0x30,0x31}

OR

PSK_KEY[]= { '1','8','0','1'}