1 // binary2cstr.cpp 2 3 #ifdef _MSC_VER 4 #pragma warning ( disable : 4786 ) 5 #endif//_MSC_VER 6 7 #include "binary2cstr.h" 8 9 using std::string; 10 11 string binary2cstr ( const string& src ) 12 { 13 string dst; 14 for ( int i = 0; i < src.size(); i++ ) 15 { 16 char c = src[i]; 17 switch ( c ) 18 { 19 case '\n': 20 dst += "\\n"; 21 break; 22 case '\r': 23 dst += "\\r"; 24 break; 25 case '\t': 26 dst += "\\t"; 27 break; 28 case '\v': 29 dst += "\\v"; 30 break; 31 case '\"': 32 dst += "\x22"; 33 break; 34 default: 35 if ( isprint ( c ) ) 36 dst += c; 37 else 38 { 39 dst += "\\x"; 40 char tmp[16]; 41 _snprintf ( tmp, sizeof(tmp)-1, "%02X", (unsigned)(unsigned char)c ); 42 tmp[sizeof(tmp)-1] = '\0'; 43 dst += tmp; 44 } 45 break; 46 } 47 } 48 return dst; 49 }