Results 1 to 10 of 10

Thread: QString to unsigned char *

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Question QString to unsigned char *

    Hi ppl,

    I need to convert a QString to a unsigned char array. I've already searched the forums & did some google reasearch but somehow failed to find a working solution

    I had no probs to convert the QString to a normal char array:

    Qt Code:
    1. QString str = "somestring"; // data comes from a db in my case
    2. int length = str.length();
    3.  
    4. char *sequence = NULL;
    5. sequence = new char[length+1];
    6. strcpy(sequence, str.toLocal8Bit());
    To copy to clipboard, switch view to plain text mode 

    i think the "unsigned" solution is also pretty simple, but somehow i can't figure it out???

    please help...

    thx

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

    Default Re: QString to unsigned char *

    Do it with QString::toAscii().constData().
    It returns a preallocated (const char *) and it is frequently used for these situations.

    Qt Code:
    1. char *sequence = string.toAscii().constData();
    To copy to clipboard, switch view to plain text mode 

    Regards

  3. The following user says thank you to marcel for this useful post:

    darksaga (21st July 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString to unsigned char *

    J-P Nurmi

  5. #4
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: QString to unsigned char *

    Quote Originally Posted by marcel View Post
    Do it with QString::toAscii().constData().
    It returns a preallocated (const char *) and it is frequently used for these situations.

    Qt Code:
    1. char *sequence = string.toAscii().constData();
    To copy to clipboard, switch view to plain text mode 

    Regards
    that was exactly what I was looking for, thx @ marcel

    now using the following code:
    Qt Code:
    1. QString str = "ABCD";
    2. int length = str.length();
    3. unsigned char *sequence = NULL;
    4. sequence = (unsigned char*)qstrdup(str.toAscii().constData());
    To copy to clipboard, switch view to plain text mode 

    looked into the QByteArray reference, and would like to know if my assumptions for the example above are correct:

    - sequence length = 5 --> ['A'] ['B'] ['C'] ['D'] ['\0']
    - sequence is now "independant" from str
    - sequence has to be deleted with -> delete [] sequence

    ????

    Regards

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

    Default Re: QString to unsigned char *

    Yes, they are correct.
    You must delete your (unsigned char*) since you own a copy of the QByteArray buffer.

    Regards

  7. #6
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: QString to unsigned char *

    Quote Originally Posted by marcel View Post
    Yes, they are correct.
    You must delete your (unsigned char*) since you own a copy of the QByteArray buffer.

    Regards


    thnx again

  8. #7
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString to unsigned char *

    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.

    To be on the safe side use code like:
    Qt Code:
    1. const QByteArray ba = string.toAscii(); // make ba const, because modifying this array might otherwise invalidate the pointer
    2. const char *sequence = ba.constData();
    3. // now sequence will remain valid within the current scope.
    To copy to clipboard, switch view to plain text mode 

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

    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

  10. #9
    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

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.