Coversion of QString to char array
-
Hi all,
I want to convert QString to char array. how can i do this ?
for eg: i have a QString Str="Qt Qml Development"
and a char text[100]={"Some text "}i want to replace Some text with the above Str
Can anyone please guide me how i can do this.
Thanks
-
Hi all,
I want to convert QString to char array. how can i do this ?
for eg: i have a QString Str="Qt Qml Development"
and a char text[100]={"Some text "}i want to replace Some text with the above Str
Can anyone please guide me how i can do this.
Thanks
@Naveen_D hi,
seems likestatic_cast<char>
should help you.I'm not sur if you can convert a QString into an char-array directly, therefore I propose this:
//Warning untested code QString myString; char text [myString.length())]; for(int i = 0; i < myString.length(); i++) text[i] = static_cast<char>(myString.at(i));
This would also be a bit easier if you used
QChar
-
@Naveen_D hi,
seems likestatic_cast<char>
should help you.I'm not sur if you can convert a QString into an char-array directly, therefore I propose this:
//Warning untested code QString myString; char text [myString.length())]; for(int i = 0; i < myString.length(); i++) text[i] = static_cast<char>(myString.at(i));
This would also be a bit easier if you used
QChar
@J.Hilk Thanks for the reply,
i tried with this i am getting the following error
error: invalid static_cast from type ‘const QChar’ to type ‘char’
text[i] = static_cast<char>(myString.at(i));error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char text[100] = {textStr};i tried like this ->
QString myString="Qt Qml"; char textStr [myString.length()]; for(int i = 0; i < myString.length(); i++) textStr[i] = static_cast<char>(myString.at(i)); char text[100] = {textStr};
-
@J.Hilk Thanks for the reply,
i tried with this i am getting the following error
error: invalid static_cast from type ‘const QChar’ to type ‘char’
text[i] = static_cast<char>(myString.at(i));error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char text[100] = {textStr};i tried like this ->
QString myString="Qt Qml"; char textStr [myString.length()]; for(int i = 0; i < myString.length(); i++) textStr[i] = static_cast<char>(myString.at(i)); char text[100] = {textStr};
-
depends on the encoding. for UTF-8:
const QByteArray stringData = myString.toUtf8(); char text[100]; text[qMin(99,stringData.size())]='\0'; std::copy(stringData.constBegin(),stringData.constBegin()+qMin(99,stringData.size()),text);
This, of course, might truncate any multi-byte character at the end of the string
char textStr [myString.length()];
this is not valid C++ syntax -
depends on the encoding. for UTF-8:
const QByteArray stringData = myString.toUtf8(); char text[100]; text[qMin(99,stringData.size())]='\0'; std::copy(stringData.constBegin(),stringData.constBegin()+qMin(99,stringData.size()),text);
This, of course, might truncate any multi-byte character at the end of the string
char textStr [myString.length()];
this is not valid C++ syntax