c++ - std::string 中的 0xc2 字符
问题描述
以下字符串的大小为 4 而不是我预期的 3。
std::string s = "\r\n½";
int ss = s.size(); //ss is 4
当逐个字符地循环字符串将其转义为十六进制时,我得到
- 0x0D(回车的十六进制代码)
- 0x0A(换行的十六进制代码)
- 0xc2(十六进制代码,但这是什么?)
- 0xbd(½ 字符的十六进制代码)
0xc2 来自哪里?它是某种编码信息吗?我虽然 std::string 在字符串中每个可见字符都有一个字符。有人可以确认 0xc2 是“字符集修饰符”吗?
解决方案
“½”在 unicode 中具有代码点U+00BD
,并由 UTF-8 由两个字节序列表示0xc2bd
。这意味着,您的字符串仅包含三个字符,但长度为四个字节。
见https://www.fileformat.info/info/unicode/char/00bd/index.htm
SO的附加阅读:std::wstring VS std::string。
推荐阅读
- python - 如何在基于多个键值对的字典列表上实现逻辑?
- css - 查找已知 RGBA 值的颜色
- spring-mvc - RestTemplate 中 MessageConverters 的顺序是什么,为什么会这样?
- swift - 关于单例单元测试的问题
- timescaledb - 如何在 timescaleDB 中执行 merge_asof?
- r - 如何将proc混合从SAS转换为R-非结构化协方差
- docker - Docker:来自守护程序的错误响应:创建临时租约时出错
- java - 使用 Java Mail API 创建 S/MIME
- javascript - 根据匹配的类和内容隐藏和显示 DIV
- reactjs - 如何防止 React / React Native 在 React Native 调试器中自动将堆栈跟踪附加到我的控制台警告