Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: How to convert QString to std::string or char*?

  1. #1
    Join Date
    Jun 2009
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to convert QString to std::string or char*?

    If my QString only has ascii character, I can convert it to std::string or char* as follow:
    Qt Code:
    1. QString x = "hello";
    2. std::string str = x.toStdString();
    3. str.c_str();
    To copy to clipboard, switch view to plain text mode 

    But if my QString has chinses character, I cannot conver it to std::string or char*.
    Qt Code:
    1. QString x = " 你 好 ";
    2. std::string str = x.toStdString(); //str will become Unrecognizable Code.
    3. str.c_str();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by yangyunzhao View Post
    Qt Code:
    1. QString x = " 你 好 ";
    2. std::string str = x.toStdString(); //str will become Unrecognizable Code.
    3. str.c_str();
    To copy to clipboard, switch view to plain text mode 
    As Qt docs says for QString::toStdString().
    If the QString contains non-ASCII Unicode characters, using this operator can lead to loss of information, since the implementation calls toAscii().
    You can use:
    Qt Code:
    1. std::wstring QString::toStdWString () const
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2009
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by yogeshgokul View Post
    As Qt docs says for QString::toStdString().

    You can use:
    Qt Code:
    1. std::wstring QString::toStdWString () const
    To copy to clipboard, switch view to plain text mode 
    Yes,if use std::wstring,it works ok.

    But in my program ,I use a third party API. The third party API request a std::string or char* parameter.

  4. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by yangyunzhao View Post
    But in my program ,I use a third party API. The third party API request a std::string or char* parameter.
    Try:
    Qt Code:
    1. string->toUtf8()->data();
    To copy to clipboard, switch view to plain text mode 
    Last edited by yogeshgokul; 24th August 2009 at 08:29.

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by yangyunzhao View Post
    Yes,if use std::wstring,it works ok.

    But in my program ,I use a third party API. The third party API request a std::string or char* parameter.
    if you want char* then forget about unicode... you cant pass a chinese character.in char*. Dont waste your time... tell your boss that char* is 7bit ascii.. (-127to 127).. so either change the third party library or live with english only.

  6. #6
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Never heard of UTF-8, have you?

    Follow yogeshgokul's advice and convert your strings to utf-8 encoding.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by franz View Post
    Never heard of UTF-8, have you?

    Follow yogeshgokul's advice and convert your strings to utf-8 encoding.
    no matter what encoding he converts his QString to ... but when eventually it be converted to a char* (by any function, toAscii(), or data(), or anything), it will loose the unicode charecter.

  8. #8
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    It will not, char* is just a pointer to memory, there will be no loss, depending how the further reading will be done.

  9. #9
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Yes, but its likely, that the author of the library would have used something other then char* if the library supports non ascii characters.

  10. #10
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by The Storm View Post
    It will not, char* is just a pointer to memory, there will be no loss, depending how the further reading will be done.
    and how do you read char* ? 8 bits at a time isn't it? so no matter how your memory is stored... the functions which manipulate char* will ALWAYS loose data (or corrupt data in real sense)

  11. #11
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    You have a long way to go...

    This function is from the Qt sources:
    Qt Code:
    1. QString QString::fromUtf8(const char *str, int size)
    2. {
    3. if (!str)
    4. return QString();
    5. if (size < 0)
    6. size = qstrlen(str);
    7.  
    8. QString result;
    9. result.resize(size); // worst case
    10. ushort *qch = result.d->data;
    11. uint uc = 0;
    12. uint min_uc = 0;
    13. int need = 0;
    14. int error = -1;
    15. uchar ch;
    16. int i = 0;
    17.  
    18. // skip utf8-encoded byte order mark
    19. if (size >= 3
    20. && (uchar)str[0] == 0xef && (uchar)str[1] == 0xbb && (uchar)str[2] == 0xbf)
    21. i += 3;
    22.  
    23. for (; i < size; ++i) {
    24. ch = str[i];
    25. if (need) {
    26. if ((ch&0xc0) == 0x80) {
    27. uc = (uc << 6) | (ch & 0x3f);
    28. need--;
    29. if (!need) {
    30. if (uc > 0xffffU && uc < 0x110000U) {
    31. // surrogate pair
    32. *qch++ = QChar::highSurrogate(uc);
    33. uc = QChar::lowSurrogate(uc);
    34. } else if ((uc < min_uc) || (uc >= 0xd800 && uc <= 0xdfff) || (uc >= 0xfffe)) {
    35. // overlong seqence, UTF16 surrogate or BOM
    36. uc = QChar::ReplacementCharacter;
    37. }
    38. *qch++ = uc;
    39. }
    40. } else {
    41. i = error;
    42. need = 0;
    43. *qch++ = QChar::ReplacementCharacter;
    44. }
    45. } else {
    46. if (ch < 128) {
    47. *qch++ = ch;
    48. } else if ((ch & 0xe0) == 0xc0) {
    49. uc = ch & 0x1f;
    50. need = 1;
    51. error = i;
    52. min_uc = 0x80;
    53. } else if ((ch & 0xf0) == 0xe0) {
    54. uc = ch & 0x0f;
    55. need = 2;
    56. error = i;
    57. min_uc = 0x800;
    58. } else if ((ch&0xf8) == 0xf0) {
    59. uc = ch & 0x07;
    60. need = 3;
    61. error = i;
    62. min_uc = 0x10000;
    63. } else {
    64. // Error
    65. *qch++ = QChar::ReplacementCharacter;
    66. }
    67. }
    68. }
    69. if (need) {
    70. // we have some invalid characters remaining we need to add to the string
    71. for (int i = error; i < size; ++i)
    72. *qch++ = QChar::ReplacementCharacter;
    73. }
    74.  
    75. result.truncate(qch - result.d->data);
    76. return result;
    77. }
    To copy to clipboard, switch view to plain text mode 

    As I told you, the data is not lost, everything is depending on how you read it. Other nice example is if you want to send int directly via network. Each function that sends that via the network takes char* pointer. Lets say we put:

    Qt Code:
    1. int i = 100;
    2. socket->write( (const char*)i, sizeof(int) );
    To copy to clipboard, switch view to plain text mode 

    And now what - the integer is lost ? On the other side you will get 4 bytes(32bit OS), the you need to make just some bit operations in order to get back the integer number.

  12. #12
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    actually we both are saying the same thing but looking at different angles...

    he you need to make just some bit operations in order to get back the integer number.
    does someAsciiSearchFunc(char*,char*) will do that correctly if you provide it with 16 bit data? it will check 8bits only...
    so yes you can play around with typecasting if own the code on both sides...

  13. #13
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    ahhh... now i got it... just read about utf8 ... looks like i really have a long way to go... time to go back to school..

  14. #14
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by MrDeath View Post
    time to go back to school..
    Yes ! school bell is ringing.

    So now what will be final outcome. What should yangyunzhao do to get the things done.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by The Storm View Post
    Lets say we put:

    Qt Code:
    1. int i = 100;
    2. socket->write( (const char*)i, sizeof(int) );
    To copy to clipboard, switch view to plain text mode 

    And now what - the integer is lost ?
    This is different. String manipulation includes a special interpretation of the \0 character. In the above case it will work because you are passing the size but when you pass a string that contains \0 characters to let's say... strcpy(), you will lose data. So it all comes down to the question if UTF-8 encoded string contains null bytes or not. From what I see it doesn't so storing UTF-8 encoded strings in char* should be safe. On the other hand if the 3rd party API does something with the data it gets, it is likely you will lose data or get incorrect outputs (i.e. the size of such "text" will be incorrect).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    i was trying to say the same thing... ummm.. my english..

  17. #17
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Then why QString class have this function.
    Qt Code:
    1. QString QString::fromUtf8 ( const char * str, int size = -1 ) [static]
    To copy to clipboard, switch view to plain text mode 

    This function takes char* parameter and function name says fromUtf8. Does it mean Qt accepting Utf8 formatted string pointed by char*.
    So it implies Qt itself saying that a Utf8 formatted string can be pointed by char*.

  18. #18
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    go and read wikipedia
    UTF-8 encodes each character (code point) in 1 to 4 octets (8-bit bytes), with the single octet encoding used only for the 128 US-ASCII characters. See the Description section below for details.

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by yogeshgokul View Post
    Then why QString class have this function.
    Qt Code:
    1. QString QString::fromUtf8 ( const char * str, int size = -1 ) [static]
    To copy to clipboard, switch view to plain text mode 

    This function takes char* parameter and function name says fromUtf8.
    "const char *" in C/C++ is "an array of 8bit values" so anything can go there. Its Qt equivalent is QByteArray. How the data stored in a particular const char * is interpreted is another story (that's why you have the "size" parameter here). UTF-8 strings CAN contain 0x00 values - but only if you directly place them there yourself. So in 99.9% of the cases keeping a utf-8 string in a C byte array without also keeping its size is fine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #20
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to std::string or char*?

    Quote Originally Posted by wysota View Post
    So in 99.9% of the cases keeping a utf-8 string in a C byte array without also keeping its size is fine.
    Then how you explain this:

    Quote Originally Posted by MrDeath View Post
    no matter what encoding he converts his QString to ... but when eventually it be converted to a char* (by any function, toAscii(), or data(), or anything), it will loose the unicode charecter.

Similar Threads

  1. Getting std::string object from QString object ( qt3)
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2013, 21:09
  2. convert from QString to char[sizeof(...)]
    By adamatic in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2011, 10:05
  3. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 14:33
  4. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 16:22
  5. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 10:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.