Hi all,
I want to be able to convert my QString in unicode format and the convert it in char*.
I have try with the following code but I have a casting problem (my QByteArray contains only the first character).
Any Suggestions?
with regards, Dax
Printable View
Hi all,
I want to be able to convert my QString in unicode format and the convert it in char*.
I have try with the following code but I have a casting problem (my QByteArray contains only the first character).
Any Suggestions?
with regards, Dax
Let's see:
That's already taken care of.Quote:
Originally Posted by the documentation
Now, to turn it into a char pointer:
Thankyou tbscope,
are you sure that this code work correctly? My debugger (VS2005) show me bad characters in myStringChars but I don't know if is a format-code problem.
You mean you actually have a unicode string?
Yes, that's correct.
Understand what a unicode string is.
That's likely to end in tears. QString::toUtf8() returns a temporary QByteArray, data() returns a pointer to its data buffer, and then the temporary QByteArray ceases to exist. Ultimately, you get a char* to a buffer inside a temporary object that no longer exists and can therefore be overwritten by other machine processes.
You need to control the scope of the QByteArray:
Code:
char *myStringChars = ba.data(); // do stuff with myStringChars // let ba go out of scope
Yes, now I remember it :-)
I knew there was something wrong with it.
Ok, thanks :)
Consider using the qPrintable macro.