d6bfb83b40cff72b75352226a89b9c816d851800
[vg.git] / vg_binstr.h
1 #ifndef VG_BINSTR
2 #define VG_BINSTR
3
4 /* dead simple.. 4 bits/character encoding */
5
6 #include "vg_stdint.h"
7
8 #define VG_BINSTR_BASECHAR 0x41
9
10 static void vg_str_bin( const void *txt, void *bin, int size )
11 {
12 const u8 *src = txt;
13 u8 *dst = bin;
14
15 for( u32 i=0; i<size/2; i++ ){
16 dst[i] = (src[i*2+0]-VG_BINSTR_BASECHAR);
17 dst[i] |= (src[i*2+1]-VG_BINSTR_BASECHAR)<<4u;
18 }
19 }
20
21 static void vg_bin_str( const void *bin, void *txt, u32 size )
22 {
23 u8 *dst = txt;
24 const u8 *src = bin;
25
26 for( u32 i=0; i<size; i++ ){
27 dst[i*2+0] = VG_BINSTR_BASECHAR + ((src[i] ) & 0xf);
28 dst[i*2+1] = VG_BINSTR_BASECHAR + ((src[i]>>4u) & 0xf);
29 }
30 }
31
32 #endif /* VG_BINSTR */