I have a need to store the hex value from a character array x[11]
to a memory space: char* content
.
The character array has contents like:
{'b', '5', 'a', '8', 'a', 'e', 'a', 'b', 'c', '7', '\n'}
And I need to save the ten elements except the \n
. The following is how I did it:
char *content = (char *)calloc(msg_len, sizeof(char));
memcpy(content , x, 10);
The memory space output ended up to be in ASCII format:
62(b) 35(5) 61(a) 38(8) 61(a) ...
But what I hoped it to show in the memory is:
b5 a8 ae ab c7
Just like what it is in character array, shown above.
What steps can I take to to solve that?
Would something like this work?
#include <stdio.h>
int main() {
char x[] = {'b', '5', 'a', '8', 'a', 'e', 'a', 'b', 'c', '7', '\n'};
unsigned long long a;
unsigned char b[5];
sscanf(x, "%llx\n", &a);
b[0] = a >> 32;
b[1] = (a >> 24);
b[2] = (a >> 16);
b[3] = (a >> 8);
b[4] = a ;
for(int i = 0; i < 5; i++) {
printf("%x ", b[i]);
}
printf("\n");
return 0;
}