PDA

View Full Version : QByteArray to QString



hvitual
12th December 2009, 03:10
I have a problem when converting QByteArray to QString and now I need a helper.

I have a file.txt contain "Công nghệ thông tin" :: it is a Vietnamese string.
I read file to byteArray (byteArray is a variable QByteArray type) .

Then, I convert it to string (string is a variable QString type).
code: QString string(byteArray);

But, occuring a error when display text "Công nghệ thông tin".
I think reason is encoding error.
I want a QString Utf8.

Please, help me, thanks. (sorry for my English level).

ketan007
12th December 2009, 04:29
hi..
you can use this function below to convert to string.

public String Byte2Array(Byte[] b){
String str="";
for(int i=0;i<=b.length(),i++){
str = str + (char)byte[i];
}
print(str);
}

Lykurg
12th December 2009, 09:36
Do you need that byte array? or is it just for reading the file?

I suggest you to use QTextStream to read the file (with a proper encoding). Then you have the content in a QString which you can convert to a byte array if needed.

squidge
12th December 2009, 10:43
You should start your QByteArray with a unicode BOM, that way QString can detect the proper encoding correctly.

ketan007: That function is built into QString.