2 * UCW Library -- Hexdumping and Unhexdumping
4 * (c) 2008 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/string.h>
12 #include <ucw/chartype.h>
17 return (x < 10) ? (x + '0') : (x - 10 + 'a');
21 mem_to_hex(char *dest, const byte *src, uns bytes, uns flags)
23 uns sep = flags & 0xff;
27 dest[0] = hex_make(*src >> 4);
28 dest[1] = hex_make(*src & 0x0f);
29 if (flags & MEM_TO_HEX_UPCASE)
31 dest[0] = Cupcase(dest[0]);
32 dest[1] = Cupcase(dest[1]);
47 return (c < 10) ? c : (c - 7);
51 hex_to_mem(byte *dest, const char *src, uns max_bytes, uns flags)
53 uns sep = flags & 0xff;
54 while (max_bytes-- && Cxdigit(src[0]) && Cxdigit(src[1]))
56 *dest++ = (hex_parse(src[0]) << 4) | hex_parse(src[1]);
58 if (sep && *src && max_bytes)
60 if (*src != (char)sep)
74 byte x[4] = { 0xfe, 0xed, 0xf0, 0x0d };
78 mem_to_hex(a, x, 4, MEM_TO_HEX_UPCASE);
80 mem_to_hex(a, x, 4, ':');
82 const char *z = hex_to_mem(y, a, 4, ':');
86 printf("%02x%02x%02x%02x\n", y[0], y[1], y[2], y[3]);