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 :confused:
I had no probs to convert the QString to a normal char array:
Code:
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
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.
Code:
char *sequence = string.toAscii().constData();
Regards
Re: QString to unsigned char *
Re: QString to unsigned char *
Quote:
Originally Posted by
marcel
Do it with QString::toAscii().constData().
It returns a preallocated (const char *) and it is frequently used for these situations.
Code:
char *sequence = string.toAscii().constData();
Regards
that was exactly what I was looking for, thx @ marcel
now using the following code:
Code:
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
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
Re: QString to unsigned char *
Quote:
Originally Posted by
marcel
Yes, they are correct.
You must delete your (unsigned char*) since you own a copy of the QByteArray buffer.
Regards
:)
thnx again ;)
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:
Code:
char *sequence = string.toAscii().constData();
// sequence is now a dangling pointer!
a call like
Code:
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:
Code:
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.
Re: QString to unsigned char *
Quote:
Originally Posted by
spud
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:
Code:
char *sequence = string.toAscii().constData();
// sequence is now a dangling pointer!
a call like
Code:
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
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
Re: QString to unsigned char *
Quote:
Originally Posted by
comlink21
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?