Conver QString to char
-
Hi,
One way to do that:
QByteArray utf8Data = myQString.toUtf8(); const char *string = utf8Data.constData(); -
What is the advantage compared to: toStdString().c_str(); ?
-
What is the advantage compared to: toStdString().c_str(); ?
-
Char only hold 1 character, to hold more need multiple
QString qstr;
qstr.toStdString().c_str(); -
Sorry, so used to the
char *use case that I didn't thought about that one.So something like
char myChar = myString[0].toLatin1()? -
Sorry, so used to the
char *use case that I didn't thought about that one.So something like
char myChar = myString[0].toLatin1()? -
@Jeronimo said in Conver QString to char:
@SGaist thx a lot for your help. it's possible to convert char for utf16??
As @SGaist said, not with
char. You'd need to use a wide char instead, such as:wchar_t myChar = myString[0].unicode();However,
wchar_tis compiler / platform dependant, and not portable when used in this way.What would you want to pass the UTF-16 character on to?
QChar::unicode()would probably work for that case.Cheers.
-
@Jeronimo said in Conver QString to char:
@SGaist thx a lot for your help. it's possible to convert char for utf16??
As @SGaist said, not with
char. You'd need to use a wide char instead, such as:wchar_t myChar = myString[0].unicode();However,
wchar_tis compiler / platform dependant, and not portable when used in this way.What would you want to pass the UTF-16 character on to?
QChar::unicode()would probably work for that case.Cheers.
@Paul-Colby said in Conver QString to char:
QChar::unicode()
solved i prefer this method:
QString cadena = "Hola";
std::string cadenaStd = cadena.toStdString();
QChar c = cadenaStd[0] or char -
Why not use
QChar c = cadena[0]?