سؤال

I need to solve the problem opposite to this one. I have QString with non-ascii symbols.
For example:
Schöne Grüße

How to get the length of the string in bytes for UTF8 case? It should be 15.
I have tried conversion to ByteArray, to Latin1, ASCII but length is always the same.

هل كانت مفيدة؟

المحلول

You need to use ::toUtf8() and append it to a QByteArray. Then you can get the length using .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

The C Standard API strlen() returns the number of bytes in a null terminated string, not including the null terminator.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top