c++ - How to read from file UTF simbols as if they were UTF code? -


so have file - html file there lots of simbols &'""""</\>9()!@#+=- need convert them form can copied output screen after passed std::string str ("here should utf simbols"); how such thing (using c++ boost)

this code assumes compiling system uses superset of ascii, reasonable on today's systems. gives string literal std::string, including surrounding quotes. input data treated generic bytes rather required utf-8.

std::string string_literal(int length, char const *data) {   std::stringstream s;   std::ostream shex (s.rdbuf());   shex << std::hex << std::uppercase;   shex.fill('0');    s << '"';   (int n = 0; n != length; ++n) {     unsigned char c = data[n];     if (c < 32 || 0x7f <= c) {       // add special cases \n, \t, \r, etc. produce nicer output       shex << "\\x" << std::setw(2) << int(c);     }     else {       switch (c) {       case '"':       case '\\':         s << '\\' << c;         break;        default:         s << c;       }     }   }   s << '"';   return s.str(); } 

example:

// string literals, makes below example easier template<int n> std::string string_literal(char const (&data)[n]) {   assert(data[n - 1] == '\0');   return string_literal(n - 1, data); }  // convenience overload std::string string_literal(std::string const &s) {   return string_literal(s.length(), s.data()); }  int main() {   std::cout << "#include <iostream>\nint main() {\n  std::cout << ";   std::cout << string_literal("&'\"</\\>9()!@#+=-") << "\n            << ";   std::cout << string_literal("☺ ☃ ٩(•̮̮̃•̃)۶") << ";\n}\n";     // first , second smiley face , snowman     // third may not display correctly on browser   return 0; } 

output:

#include <iostream> int main() {   std::cout << "&'\"</\\>9()!@#+=-"             << "\xe2\x98\xba \xe2\x98\x83 \xd9\xa9(\xe2\x80\xa2\xcc\xae\xcc\xae\xcc\x83\xe2\x80\xa2\xcc\x83)\xdb\xb6"; } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -