Results 1 to 6 of 6

Thread: Convertion from QString to unsigned char *

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Convertion from QString to unsigned char *

    Quote Originally Posted by vcp View Post
    Qt Code:
    1. ucTest = (unsigned char *) str.toLatin1().data();
    To copy to clipboard, switch view to plain text mode 
    Nope, that is not correct.

    QString::data() doesn't return what you think it returns.
    Use this instead:

    Qt Code:
    1. ucTest = (unsigned char *) str.toAscii().data();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Convertion from QString to unsigned char *

    Then, what exactly QString::data() returns?

    tnx

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convertion from QString to unsigned char *

    str.toAscii().data() is the same as str.toLatin1().data unless you use setCodecForCStrings().

    Note that there is a temporary object being created here, so the return value isn't valid when the statement is complete. So:
    Qt Code:
    1. ucTest = (unsigned char *) str.toLatin1().data();
    To copy to clipboard, switch view to plain text mode 

    Will never work correctly, but this will:
    Qt Code:
    1. myFunc((unsigned char *) str.toLatin1().data());
    To copy to clipboard, switch view to plain text mode 
    myFunc will be passed a pointer which will not be deleted until after myFunc returns.

    If you want to pointer to remain valid, create a QByteArray:
    Qt Code:
    1. QByteArray ascii = str.toLatin1();
    2. ucTest = (unsigned char *) ascii.data();
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to squidge for this useful post:

    vcp (13th August 2010)

  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Convertion from QString to unsigned char *

    Quote Originally Posted by fatjuicymole View Post
    str.toAscii().data() is the same as str.toLatin1().data unless you use setCodecForCStrings().
    Indeed. Hmm, I did look at the QString documentation and I can swear that the I read the toLatin1() returned a QString. Strange. Sorry about the confusion.

  6. #5
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Convertion from QString to unsigned char *

    Dear fatjuicymole,

    His explanation and example were really helpful!
    Now I understand perfectly that issue, and certainly
    was extremely helpful to me.

    Thank you very much

    I hope at some point could also help with my
    knowledge in this way.

    Thanks again.

Similar Threads

  1. convert from unsigned char* to QString
    By bhaskar in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2010, 06:36
  2. unsigned char * to QString
    By elina.du in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2009, 08:33
  3. convert unsigned char * to QString
    By sepehr in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 20:31
  4. QString to unsigned char *
    By darksaga in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2007, 07:52
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22: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
  •  
Qt is a trademark of The Qt Company.