简体   繁体   中英

c++ how to print utf8 symbol using utf8 hex code?

For example I have utf8 hex code of a symbol which is:

e0a4a0

how can I use this hex code to print this character to a file in c++?

I see two approaches you can take: first is to use it literally as "ठ" second is to encode it in escape sequence \\u\u003c/code> or \\x (both doesn't work, so I don't have an example)

std::cout<<"ठ\xe0\xa4\xa0"<<std::endl;

But I think that best option for you will be open file in binary mode and simply put those bytes there.

You need to output each byte separately.

For example if you have:

c386

You need to convert first byte (first two characters "c3") to int, convert to char and output, then second byte ("86") and also convert and output.

Conversion for this particular case ("c386"):

string a="c386";
char b = (a[0] - 'A' + 10) * 16 + a[1] - '0';
char c = (a[2] - '0') * 16 + a[3] - '0';
cout << b << c << endl;

"c3" = 44*16 + 3 = 707, "86" = 8*16 + 6 = 134. Convert those int values to char and output them both and you have your character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM