首页 > 解决方案 > std::string 中的 0xc2 字符

问题描述

以下字符串的大小为 4 而不是我预期的 3。

std::string s = "\r\n½"; 
int ss = s.size(); //ss is 4

当逐个字符地循环字符串将其转义为十六进制时,我得到

0xc2 来自哪里?它是某种编码信息吗?我虽然 std::string 在字符串中每个可见字符都有一个字符。有人可以确认 0xc2 是“字符集修饰符”吗?

标签: c++charstdstring

解决方案


“½”在 unicode 中具有代码点U+00BD,并由 UTF-8 由两个字节序列表示0xc2bd。这意味着,您的字符串仅包含三个字符,但长度为四个字节。

https://www.fileformat.info/info/unicode/char/00bd/index.htm

SO的附加阅读:std::wstring VS std::string


推荐阅读