reduction
[vg.git] / vg_binstr.h
diff --git a/vg_binstr.h b/vg_binstr.h
new file mode 100644 (file)
index 0000000..da41ce0
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef VG_BINSTR
+#define VG_BINSTR
+
+/* dead simple.. 4 bits/character encoding */
+
+#include "vg_stdint.h"
+
+#define VG_BINSTR_BASECHAR 0x41
+
+static void vg_str_bin( const void *txt, void *bin, int size )
+{
+   const u8 *src = txt;
+         u8 *dst = bin;
+
+   for( u32 i=0; i<size/2; i++ )
+   {
+      dst[i]  = (src[i*2+0]-VG_BINSTR_BASECHAR);
+      dst[i] |= (src[i*2+1]-VG_BINSTR_BASECHAR)<<4u;
+   }
+}
+
+static void vg_bin_str( const void *bin, void *txt, u32 size )
+{
+         u8 *dst = txt;
+   const u8 *src = bin;
+
+   for( u32 i=0; i<size; i++ )
+   {
+      dst[i*2+0] = VG_BINSTR_BASECHAR + ((src[i]    ) & 0xf);
+      dst[i*2+1] = VG_BINSTR_BASECHAR + ((src[i]>>4u) & 0xf);
+   }
+}
+
+#endif /* VG_BINSTR */