简体   繁体   English

C ++如何使用utf8十六进制代码打印utf8符号?

[英]c++ how to print utf8 symbol using utf8 hex code?

For example I have utf8 hex code of a symbol which is: 例如,我有一个符号的utf8十六进制代码,它是:

e0a4a0

how can I use this hex code to print this character to a file in c++? 我该如何使用这个十六进制代码打印这个人物在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) 我看到您可以采取两种方法:第一种是将其字面使用为"ठ"第二种是以转义序列\\u\u003c/code>或\\x对其进行编码(两者均无效,因此我没有示例)

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. 您需要将第一个字节(前两个字符“ c3”)转换为int,转换为char并输出,然后将第二个字节(“ 86”)转换为输出。

Conversion for this particular case ("c386"): 针对这种情况的转换(“ 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. “ c3” = 44 * 16 + 3 = 707,“ 86” = 8 * 16 + 6 =134。将这些int值转换为char并输出它们,您便拥有了自己的角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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