Skip to content

Instantly share code, notes, and snippets.

@KennFatt
Created May 22, 2020 11:28
Show Gist options
  • Save KennFatt/164cd1907e3b797a205640b6c3e90a83 to your computer and use it in GitHub Desktop.
Save KennFatt/164cd1907e3b797a205640b6c3e90a83 to your computer and use it in GitHub Desktop.
Convert hex string to byte array
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
const char raw_payload[] = "11ff0e3b0001ff00ff0000000000";
const char *pos = raw_payload;
unsigned char payload[14];
size_t cnt = 0;
for (; cnt < sizeof(payload) / sizeof(payload[0]); ++cnt) {
char buff[5] = {'0', 'x', pos[0], pos[1], 0};
payload[cnt] = strtol(buff, NULL, 0);
pos += 2 * sizeof(char);
}
printf("0x");
for (cnt = 0; cnt < sizeof(payload) / sizeof(payload[0]); ++cnt) {
printf("%02x", payload[cnt]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment