本文介紹了獲取 UTF-8 編碼的 std::string 的實際長度?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我的 std::string 顯然是 utf-8 編碼的,str.length() 返回了錯誤的結果.
my std::string is utf-8 encoded so obviously, str.length() returns the wrong result.
我找到了此信息,但我不確定如何使用它來執行此操作:
I found this information but I'm not sure how I can use it to do this:
以下字節序列是用來表示一個字符.這順序是使用取決于字符的 UCS 代碼編號:
The following byte sequences are used to represent a character. The sequence to be used depends on the UCS code number of the character:
0x00000000 - 0x0000007F:
0xxxxxxx
0x00000080 - 0x000007FF:
110xxxxx 10xxxxxx
0x00000800 - 0x0000FFFF:
1110xxxx 10xxxxxx 10xxxxxx
0x00010000 - 0x001FFFFF:
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
如何找到 UTF-8 編碼的 std::string 的實際長度?謝謝
How can I find the actual length of a UTF-8 encoded std::string? Thanks
推薦答案
計算所有首字節(與 10xxxxxx 不匹配的字節).
Count all first-bytes (the ones that don't match 10xxxxxx).
int len = 0;
while (*s) len += (*s++ & 0xc0) != 0x80;
這篇關于獲取 UTF-8 編碼的 std::string 的實際長度?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!