C ++:将UTF16 char的十六进制表示转换为十进制(如python的int(hex

编程入门 行业动态 更新时间:2024-10-21 13:05:50
本文介绍了C ++:将UTF16 char的十六进制表示转换为十进制(如python的int(hex_data,16))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我找到了一个将十六进制表示法解码为十进制数的解释,但只使用Qt:

由于我不使用Qt和 cout<< (int)c does not work (编辑:如果您正确使用它,它实际上可以正常工作..!):

如何做到以下几点:

两个字符是通过某个套接字传输的(只是想出了如何最终得到十六进制repr!),并且两者都是在 utf16表示形式之后合成的:

char c = u\0b7f

这将被转换为 2943 的utf16十进制值! (见utf-table http:// www .fileformat.info / info / unicode / char / 0b7f / index.htm )这应该是绝对基本的东西,但作为一个指定的Python开发人员被迫在一个项目中使用C ++我将这个问题悬挂了数小时....

解决方案

使用更宽的字符类型( char 只有8位,你至少需要16位),以及正确的UTC文字格式。这可以工作(现场演示):

#include< iostream> int main() { char16_t c = u'\\\୿'; std :: cout<< (int)c<<的std :: ENDL; //如预期的那样输出是2943 return 0; }

I found an explanation to decode hex-representations into decimal but only by using Qt: How to get decimal value of a unicode character in c++

As I am not using Qt and cout << (int)c does not work (Edit: it actually does work if you use it properly..!):

How to do the following:

I got the hex representation of two chars which were transmitted over some socket (Just figured out how to get the hex repr finally!..) and both combined yield following utf16-representation:

char c = u"\0b7f"

This shall be converted into it's utf16 decimal value of 2943! (see it at utf-table www.fileformat.info/info/unicode/char/0b7f/index.htm)

This should be absolut elementary stuff, but as a designated Python developer compelled to use C++ for a project I am hanging this issue for hours....

解决方案

Use a wider character type (char is only 8 bits, you need at least 16), and also the correct format for UTC literals. This works (live demo):

#include <iostream> int main() { char16_t c = u'\u0b7f'; std::cout << (int)c << std::endl; //output is 2943 as expected return 0; }

更多推荐

C ++:将UTF16 char的十六进制表示转换为十进制(如python的int(hex

本文发布于:2023-10-23 05:49:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   十六进   char   hex   int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!