|
- #include "config.h"
- #include "util.h"
-
- /* Convert from ASCII hex. Returns
- the value, or 16 if it was space/newline, or
- 32 if some other character. */
- uint8_t from_hex(uint8_t ch)
- {
- if(ch==' ' || ch=='\r' || ch=='\n')
- return 16;
-
- if(ch < '0')
- goto bad;
- if(ch <= '9')
- return ch - '0';
- ch |= 0x20;
- if(ch < 'a')
- goto bad;
- if(ch <= 'f')
- return ch - 'a' + 10;
- bad:
- return 32;
- }
-
- const uint8_t hex[16]={'0','1','2','3','4','5','6','7',
- '8','9','a','b','c','d','e','f'};
-
- /* Convert 4 ASCII hex digits into a 16-bit unsigned number.
- Returns the number, or -1 if there's an error */
- int32_t hex_to_u16(char x[4])
- {
- uint16_t v = 0;
- uint8_t t;
- int i;
-
- for (i = 0; i < 4; i++) {
- t = from_hex(x[i]);
- if (t >= 16)
- return -1;
- v = (v << 4) | t;
- }
- return v;
- }
|