从binary text document中读取一个个字节,转化为两个字符带一个空格,……
从你所谓的很么hexcode text document中读取一个个两长度的字符串,转化为一个字节,……
程序代码:
#include <stdio.h> #include <assert.h> void b2h( const char* b, char* h ) { const char* map_ = "0123456789ABCDEF"; for( ; *b; ++b ) { *h++ = map_[(unsigned char)*b/0x10]; *h++ = map_[(unsigned char)*b%0x10]; *h++ = ' '; } *h = '\0'; } void h2b( const char* h, char* b ) { while( *h ) { unsigned char sp = 0; for( size_t i=0; i!=2; ++h, ++i ) { assert( *h ); switch( *h ) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': sp = (sp*0x10) | (*h-'0'); break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': sp = (sp*0x10) | (*h-'A'+10); break; default: assert( 0 ); } } *b++ = sp; if( !*h ) break; assert( *h == ' ' ); ++h; } *b = '\0'; } int main( void ) { { const char* bincode = "a中b"; char hexcode[100]; b2h( bincode, hexcode ); puts( hexcode ); // 输出 61 D6 D0 62 } { const char* hexcode = "61 D6 D0 62"; char bincode[100]; h2b( hexcode, bincode ); puts( bincode ); // a中b } }