Results 1 to 10 of 10

Thread: QString to unsigned char *

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: QString to unsigned char *

    Quote Originally Posted by spud View Post
    There is one gotcha with the code above!
    The call to toAscii() creates a temporary QByteArray which goes out of scope when used like this:
    Qt Code:
    1. char *sequence = string.toAscii().constData();
    2. // sequence is now a dangling pointer!
    To copy to clipboard, switch view to plain text mode 
    a call like
    Qt Code:
    1. qstrdup(str.toAscii().constData());
    To copy to clipboard, switch view to plain text mode 
    will work, though, since the the pointer isn't accessed after the QByteArray goes out of scope.
    [/code]
    Correct, of course.
    I just wrote the code here, didn't tested it anywhere.

    I think it was easy to spot in a program because probably a memory violation would have been raised as soon as the pointer was read the first time.

    Regards

  2. #2
    Join Date
    Jul 2007
    Posts
    8
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QString to unsigned char *

    try the following un-tested code;

    QString str ="ABCDEFGHIJK";
    unsigned char* uca;

    uca = new unsigned char[str.length()];
    for(int i=0; i<str.length(); i++) uca[i] = str.substr(i,1);

    cout<<uca<<endl;

    delete [] uca;

    comlink21

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: QString to unsigned char *

    Quote Originally Posted by comlink21 View Post
    try the following un-tested code;

    QString str ="ABCDEFGHIJK";
    unsigned char* uca;

    uca = new unsigned char[str.length()];
    for(int i=0; i<str.length(); i++) uca[i] = str.substr(i,1);

    cout<<uca<<endl;

    delete [] uca;

    comlink21
    What is your point?

Similar Threads

  1. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  2. QString to Char
    By kenny_isles in forum Newbie
    Replies: 1
    Last Post: 23rd February 2007, 09:51
  3. QString to const char
    By TheRonin in forum Newbie
    Replies: 2
    Last Post: 12th February 2007, 14:43
  4. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20:49
  5. QString to char
    By boss_bhat in forum Qt Programming
    Replies: 7
    Last Post: 20th July 2006, 23:26

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.