size_t hex_decode(uint8_t* buf, size_t len, const char* hex) { unsigned char c = '0'; // any non-null character size_t i; for (i=0; i<len && c; i++) { uint8_t b; if (!(c = *hex++)) break; b = hex_nibble(c)<<4; if (c) { c = *hex++; b |= hex_nibble(c); } *buf++ = b; } return i; }
static char *write_hex(char *str, uintptr_t val) { int nibbles = (sizeof(uintptr_t) * 2); for(int i = nibbles - 1; i >= 0; i--) { str[i] = hex_nibble(val & 0xf); val >>= 4; } return str + nibbles; }
bytestring_t* bytestring_new_from_hex(const char *hex) { bytestring_t *dat=bytestring_new(); unsigned hex_len=strlen(hex); unsigned c; size_t i=0; bytestring_reserve(dat,hex_len/2); while (i<hex_len) { if (is_blank(hex[i])) { i++; continue; } c=(hex_nibble(hex[i])<<4) | hex_nibble(hex[i+1]); if (c>255) return dat; bytestring_pushback(dat,(unsigned char)c); i+=2; } return dat; }
u8 hex_to_byte(const char *cmd) { return hex_nibble(cmd[1]) | (hex_nibble(cmd[0])<<4); }