0

Is there any way to detect std::string encoding?

My problem: I have an external web services which give data in different encodings. Also I have a library witch parse that data and store it in std::string. Than I want to display data in Qt GUI. The problem is that std::string can have different encodings. Some string can be converted using QString::fromAscii(), some QString::fromUtf8().

Ruslan F.
  • 5,498
  • 3
  • 23
  • 42

1 Answers1

0

I haven't looked into it but I did use some Qt3.3 in the past.

ASCII vs Unicode + UTF-8

Utf8 is 8-bit, ascii 7-bit. I guess you can try to look into the values of string array

http://doc.qt.digia.com/3.3/qstring.html#ascii and http://doc.qt.digia.com/3.3/qstring.html#utf8 it seems ascii returns an 8-bit ASCII representation of the string, still I think it should have values from 0 to 127 or something like that. you must compare more characters in the string.

Community
  • 1
  • 1
iNyuu
  • 62
  • 6
  • `void print_values(const char* in_string) { printf("{"); for (int i= 0; in_string[i]!= 0; i++) { printf("%d", in_string[i]); if (in_string[i+1] != 0) printf(", "); } printf("}\n"); } int main(int argc, char** argv) { QString test("iNyuu"); const char* ascii = test.ascii(); const char* utf8 = test.utf8(); printf("ascii = "); print_values(ascii); printf("utf8 = "); print_values(utf8); return 0; }` // never mind, I see "QCString utf8 () const" (utf doesn't return char array here & not sure for Qt4 // you used "operator std::string () const"? // try similar stuff with std::string – iNyuu Mar 15 '15 at 11:11