Skip to content

Instantly share code, notes, and snippets.

@alanduan
Created August 14, 2016 08:41
Show Gist options
  • Save alanduan/00b953084e47b0da490fb94fbf70e96a to your computer and use it in GitHub Desktop.
Save alanduan/00b953084e47b0da490fb94fbf70e96a to your computer and use it in GitHub Desktop.
int8_t c2hex(int8_t c)
{
return (unsigned) (c - '0') < 10 ? c - '0' : ((unsigned) (c | 0x20) - 'a' < 6 ? 9 + (c&7) : -1);
}
int8_t _c2hex(int8_t c)
{
if (c - '0' >= 0 && c - '0' < 10) return c - '0';
if (c - 'a' >= 0 && c - 'a' < 7) return c - 'a' + 10;
if (c - 'A' >= 0 && c - 'A' < 7) return c - 'A' + 10;
return -1;
}
#define c2hex(_c) \
((unsigned) ((_c) - '0') < 10 ? (_c) - '0' : ((unsigned)((_c) | 0x20) - 'a' < 6 ? 9 + ((_c)&7) : -1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment