std :: string中的0xc2字符

Joh*_*han 0 c++ char stdstring

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

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

当循环通过字符串字符逐个字符转换为十六进制时我得到

  • 0x0D(回车的十六进制代码)
  • 0x0A(换行的十六进制代码)
  • 0xc2(十六进制代码,但这是什么?)
  • 0xbd(½字符的十六进制代码)

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

YSC*_*YSC 10

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

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

另外阅读SO:std :: wstring VS std :: string.