[Qt-interest] howto convert QString to char*

Matthias Pospiech matthias.pospiech at gmx.de
Tue Jul 13 18:05:53 CEST 2010


Matthias Pospiech schrieb:
> Matthias Pospiech schrieb:
>   
>> I am using the following:
>>
>>     char * toChar(const QString str)
>>     {
>>         QByteArray ba = str.toLatin1();
>>         return  ba.data();
>>     }
>>
>> the bytearray contains usefull data,
>> however the value returned by data() is very useless.
>>   
>>     
> Just as an example:
> The original String "1.000" is converted to
> 0x957c944 
> "\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\a"
>   
Sorry for talking to myself...

If I do a (just to not use bytearray, but that makes no difference)
    char *str = const_cast<char*>(strMovement.toStdString().c_str());
    char *str2 ="1.234";

then the result is still:
0x957e284 
"\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\356\376\220"
0x67a5e03b "1.234"

an obviously the 'str' should look like str2.

Matthias





More information about the Qt-interest-old mailing list