Results 1 to 6 of 6

Thread: Convertion from QString to unsigned char *

  1. #1
    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 Convertion from QString to unsigned char *

    Hi,

    Sorry to bring this issue to light again, but I need to fix this.

    I'm with a problem to convert one type QString to unsigned char *.
    I've been reading various documents about this issue and making tests for days, but can not solve the problem.

    Here's the situation:

    Qt Code:
    1. QString str = "NONONONONO";
    2. unsigned char * ucTest;
    3. ucTest = (unsigned char *) str.toLatin1().data();
    To copy to clipboard, switch view to plain text mode 

    The result of the conversion seems to work correctly.
    But when I pass the variable to the process ucTest result is not correct!

    Now, if I define directly and pass it through the same process, so now it works.

    Qt Code:
    1. unsigned char * ucText = "NONONONONO";
    To copy to clipboard, switch view to plain text mode 

    now I pass the variable to the process the result is correct!

    Why does this happen?
    Please any of you could try to help me solve this?

    Thank a lot.

  2. #2
    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 

  3. #3
    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

  4. #4
    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 

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

    vcp (13th August 2010)

  6. #5
    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.

  7. #6
    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.