PDA

View Full Version : QString to unsigned char *



darksaga
21st July 2007, 21:28
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 :confused:

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



QString str = "somestring"; // data comes from a db in my case
int length = str.length();

char *sequence = NULL;
sequence = new char[length+1];
strcpy(sequence, str.toLocal8Bit());


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

please help...

thx

marcel
21st July 2007, 21:31
Do it with QString::toAscii().constData().
It returns a preallocated (const char *) and it is frequently used for these situations.



char *sequence = string.toAscii().constData();


Regards

jpn
21st July 2007, 22:03
How can I convert a QString to char* and vice versa ? (http://trolltech.com/developer/knowledgebase/faq.2007-01-30.9032238253/)

darksaga
21st July 2007, 22:47
Do it with QString::toAscii().constData().
It returns a preallocated (const char *) and it is frequently used for these situations.



char *sequence = string.toAscii().constData();


Regards

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

now using the following code:


QString str = "ABCD";
int length = str.length();
unsigned char *sequence = NULL;
sequence = (unsigned char*)qstrdup(str.toAscii().constData());


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

marcel
21st July 2007, 22:50
Yes, they are correct.
You must delete your (unsigned char*) since you own a copy of the QByteArray buffer.

Regards

darksaga
21st July 2007, 22:57
Yes, they are correct.
You must delete your (unsigned char*) since you own a copy of the QByteArray buffer.

Regards

:)

thnx again ;)

spud
22nd July 2007, 15:26
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:


char *sequence = string.toAscii().constData();
// sequence is now a dangling pointer!

a call like
qstrdup(str.toAscii().constData()); 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:


const QByteArray ba = string.toAscii(); // make ba const, because modifying this array might otherwise invalidate the pointer
const char *sequence = ba.constData();
// now sequence will remain valid within the current scope.

marcel
22nd July 2007, 17:47
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:


char *sequence = string.toAscii().constData();
// sequence is now a dangling pointer!
a call like
qstrdup(str.toAscii().constData()); 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

comlink21
23rd July 2007, 08:45
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

marcel
23rd July 2007, 08:52
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?