da41ce041a74881a01c9b63409ba6cc5a853f591
[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 {
17 dst[i] = (src[i*2+0]-VG_BINSTR_BASECHAR);
18 dst[i] |= (src[i*2+1]-VG_BINSTR_BASECHAR)<<4u;
19 }
20 }
21
22 static void vg_bin_str( const void *bin, void *txt, u32 size )
23 {
24 u8 *dst = txt;
25 const u8 *src = bin;
26
27 for( u32 i=0; i<size; i++ )
28 {
29 dst[i*2+0] = VG_BINSTR_BASECHAR + ((src[i] ) & 0xf);
30 dst[i*2+1] = VG_BINSTR_BASECHAR + ((src[i]>>4u) & 0xf);
31 }
32 }
33
34 #endif /* VG_BINSTR */