]> mj.ucw.cz Git - libucw.git/commitdiff
Hexdumping functions can also use separators.
authorMartin Mares <mj@ucw.cz>
Wed, 25 Jun 2008 19:17:08 +0000 (21:17 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 25 Jun 2008 19:17:08 +0000 (21:17 +0200)
lib/str-hex.c
lib/string.h

index 3dd3e438ec4ee090d43ed94ce68dac73ccd51236..8485f0c8baaacf61da958a2b392536032cedbec6 100644 (file)
@@ -18,12 +18,14 @@ hex_make(uns x)
 }
 
 void
-mem_to_hex(char *dest, const byte *src, uns bytes)
+mem_to_hex(char *dest, const byte *src, uns bytes, uns sep)
 {
   while (bytes--)
     {
       *dest++ = hex_make(*src >> 4);
       *dest++ = hex_make(*src & 0x0f);
+      if (sep && bytes)
+       *dest++ = sep;
       src++;
     }
   *dest = 0;
@@ -38,12 +40,18 @@ hex_parse(uns c)
 }
 
 const char *
-hex_to_mem(byte *dest, const char *src, uns max_bytes)
+hex_to_mem(byte *dest, const char *src, uns max_bytes, uns sep)
 {
   while (max_bytes-- && Cxdigit(src[0]) && Cxdigit(src[1]))
     {
       *dest++ = (hex_parse(src[0]) << 4) | hex_parse(src[1]);
       src += 2;
+      if (sep && *src && max_bytes)
+       {
+         if (*src != (char)sep)
+           return src;
+         src++;
+       }
     }
   return src;
 }
@@ -56,14 +64,15 @@ int main(void)
 {
   byte x[4] = { 0xfe, 0xed, 0xf0, 0x0d };
   byte y[4];
-  char a[10];
+  char a[16];
 
-  mem_to_hex(a, x, 4);
+  mem_to_hex(a, x, 4, ':');
   puts(a);
-  const char *z = hex_to_mem(y, a, 4);
+  const char *z = hex_to_mem(y, a, 4, ':');
   if (*z)
-    return 1;
-  printf("%02x%02x%02x%02x\n", y[0], y[1], y[2], y[3]);
+    puts("BAD");
+  else
+    printf("%02x%02x%02x%02x\n", y[0], y[1], y[2], y[3]);
 
   return 0;
 }
index 6b0ed593e17a2579d7dd57b109337d8290417b65..409d17fa0e7a227d826722af85ff8214b1e79a8a 100644 (file)
@@ -31,8 +31,8 @@ int str_match_pattern_nocase(const char *patt, const char *str);
 
 /* str-hex.c */
 
-void mem_to_hex(char *dest, const byte *src, uns bytes);
-const char *hex_to_mem(byte *dest, const char *src, uns max_bytes);
+void mem_to_hex(char *dest, const byte *src, uns bytes, uns sep);
+const char *hex_to_mem(byte *dest, const char *src, uns max_bytes, uns sep);
 
 /* md5hex.c */