获取以字节(而非字符)为单位的QString长度



我需要解决与此相反的问题。我有带有非ascii符号的QString
例如:
Schöne Grüße

如何获得UTF8情况下字符串的字节长度?应该是15。
我尝试过转换为ByteArrayLatin1ASCII,但长度始终相同。

您需要使用:toUtf8()并将其附加到QByteArray。然后可以使用.size().获取长度

QString s = "Schöne Grüße";
QByteArray bytes = s.toUtf8();
int length = bytes.size(); //Number of bytes

http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qbytearray.html#size

http://clc-wiki.net/wiki/C_standard_library:string.h:strlen

C Standard API strlen()返回以null结尾的字符串中的字节数,不包括null终止符。

最新更新