C ++:将UTF16 char的十六进制表示转换为十进制(如python' s int(hex_data,16))

时间:2016-11-11 11:14:58

标签: c++ hex decimal decode utf-16

我找到了将十六进制表示解码为十进制的解释,但只能使用Qt: How to get decimal value of a unicode character in c++

由于我没有使用Qt而且cout << (int)c 工作(编辑:如果您正确使用它,它确实有用..!):< / p>

如何执行以下操作:

我得到了两个字符的十六进制表示形式,它们通过某个套接字传输(只是弄清楚如何最终得到十六进制的repr!..)并且两者结合后产生 utf16-representation

char c = u"\0b7f"

这将被转换为 2943 的utf16十进制值! (见utf-table http://www.fileformat.info/info/unicode/char/0b7f/index.htm

这应该是绝对的基本内容,但作为一个指定的Python开发人员被迫使用C ++进行项目,我将这个问题挂了好几个小时....

1 个答案:

答案 0 :(得分:1)

使用更宽的字符类型(char只有8位,您需要至少16位),以及UTC文字的正确格式。这有效(live demo):

#include <iostream>

int main()
{
    char16_t c = u'\u0b7f';

    std::cout << (int)c << std::endl;  //output is 2943 as expected

    return 0;
}