unsigned int atoh(char * buf) { unsigned int retval = 0; char * cp; char digit; cp = buf; /* skip past spaces and tabs */ while (*cp == ' ' || *cp == 9) cp++; /* while we see digits and the optional 'x' */ while (ishexdigit(digit = *cp++) || (digit == 'x')) { /* its questionable what we should do with input like '1x234', * or for that matter '1x2x3', what this does is ignore all */ if (digit == 'x') retval = 0; else retval = (retval << 4) + hexnibble(digit); } return retval; }
static int hex2bin(uint8_t *buf, size_t buflen, const char *str) { int hi, lo; while(*str) { if(buflen == 0) return -1; if((hi = hexnibble(*str++)) == -1) return -1; if((lo = hexnibble(*str++)) == -1) return -1; *buf++ = hi << 4 | lo; buflen--; } return 0; }
static uint32_t makecolor(const char *str) { uint8_t r, g, b; if(*str == '#') str++; if(strlen(str) == 3) { r = hexnibble(str[0]) * 0x11; g = hexnibble(str[1]) * 0x11; b = hexnibble(str[2]) * 0x11; } else if(strlen(str) == 6) { r = (hexnibble(str[0]) << 4) | hexnibble(str[1]); g = (hexnibble(str[2]) << 4) | hexnibble(str[3]); b = (hexnibble(str[4]) << 4) | hexnibble(str[5]); } else return 0; return b << 16 | g << 8 | r; }
static int parse_bgr(const char *str) { int bgr; if(*str == '#') str++; if(strlen(str) == 6) { bgr = hexnibble(str[0]) << 4; bgr |= hexnibble(str[1]); bgr |= hexnibble(str[2]) << 12; bgr |= hexnibble(str[3]) << 8; bgr |= hexnibble(str[4]) << 20; bgr |= hexnibble(str[5]) << 16; return bgr; } return 0; }