Skip to content

Instantly share code, notes, and snippets.

@JavaCS3
Created March 27, 2020 09:38
Show Gist options
  • Save JavaCS3/493cb20c1e4e1bc6f9e8d891077f0174 to your computer and use it in GitHub Desktop.
Save JavaCS3/493cb20c1e4e1bc6f9e8d891077f0174 to your computer and use it in GitHub Desktop.
UUID String to C Array
#include <stdio.h>
#include <stdlib.h>
#define CHAR2INT(x) (('0' <= x && x <= '9') ? \
(x - '0') : \
(('a' <= x && x <= 'f') ? \
(10 + (x - 'a')) : \
(('A' <= x && x <= 'F') ? (10 + (x - 'A')) : (0))))
#define UUID2ARRAY(uuid) { \
16 * CHAR2INT(uuid[6]) + CHAR2INT(uuid[7]), \
16 * CHAR2INT(uuid[4]) + CHAR2INT(uuid[5]), \
16 * CHAR2INT(uuid[2]) + CHAR2INT(uuid[3]), \
16 * CHAR2INT(uuid[0]) + CHAR2INT(uuid[1]), \
\
16 * CHAR2INT(uuid[11]) + CHAR2INT(uuid[12]), \
16 * CHAR2INT(uuid[9]) + CHAR2INT(uuid[10]), \
\
16 * CHAR2INT(uuid[16]) + CHAR2INT(uuid[17]), \
16 * CHAR2INT(uuid[14]) + CHAR2INT(uuid[15]), \
\
16 * CHAR2INT(uuid[19]) + CHAR2INT(uuid[20]), \
16 * CHAR2INT(uuid[21]) + CHAR2INT(uuid[22]), \
\
16 * CHAR2INT(uuid[24]) + CHAR2INT(uuid[25]), \
16 * CHAR2INT(uuid[26]) + CHAR2INT(uuid[27]), \
16 * CHAR2INT(uuid[28]) + CHAR2INT(uuid[29]), \
16 * CHAR2INT(uuid[30]) + CHAR2INT(uuid[31]), \
16 * CHAR2INT(uuid[32]) + CHAR2INT(uuid[33]), \
16 * CHAR2INT(uuid[34]) + CHAR2INT(uuid[35]), \
}
#define N 16
int main(int argc, char const *argv[]) {
unsigned char test[N] = {0x11,0xb0,0xd7,0x97,0xda,0x54,0x35,0x48,0xb3,0xc4,0x91,0x7a,0xd6,0xe7,0x3d,0x74};
unsigned char arr[N] = UUID2ARRAY("97D7B011-54DA-4835-B3C4-917AD6E73D74");
for (int i = 0; i < N; i++) {
printf("0x%x ", arr[i]);
}
printf("\n");
int ret = memcmp(test, arr, N);
if (ret == 0) {
printf("PASS\n");
} else {
printf("FAILED\n");
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment