5859390743ceecc3c2b79c05340f77e05dfefe23 galt Wed Feb 13 16:50:07 2013 -0800 adding functions to decode hexstring to byte diff --git src/lib/hex.c src/lib/hex.c index c0ab199..54b8b4a 100644 --- src/lib/hex.c +++ src/lib/hex.c @@ -1,28 +1,47 @@ /* Handy hexidecimal functions * If you don't want to use printf */ #include "common.h" char nibbleToHex(char n) /* convert nibble to hexidecimal character. 0 <= n <= 15. */ { return n + ( n <= 9 ? '0' : ('a'-10) ); } void byteToHex(unsigned char n, char *hex) /* convert byte to hexidecimal characters. 0 <= n <= 255. */ { *hex++ = nibbleToHex(n >> 4); *hex++ = nibbleToHex(n & 0xf); } char *byteToHexString(unsigned char n) /* convert byte to hexidecimal string. 0 <= n <= 255. */ { char hex[3]; byteToHex(n, hex); hex[2] = 0; return cloneString(hex); } +/* And the reverse functions: */ + +char hexToNibble(char n) +/* convert hexidecimal character to nibble. 0-9a-f. */ +{ +return n - ( n <= '9' ? '0' : ('a'-10) ); +} + + +unsigned char hexToByte(char *hex) +/* convert byte to hexidecimal characters. 0 <= n <= 255. */ +{ +unsigned char n = hexToNibble(*hex++); +n <<= 4; +n += hexToNibble(*hex++); +return n; +} + +