Unsolved transforming QString to microsoft's CString, and CString to QString
-
is there a perfect solution to Conver between QString and CString?
like this ?
QString to CString: toLocal8Bit for multibyte encoding, toWString for unicode encoding.
CString to QString: fromLocal8Bit for multibyte encoding, fromWString for unicode encoding.and when to use toLatin1()?
when to use toUtf8()? -
@opengpu said in transforming QString to microsoft's CString, and CString to QString:
and when to use toLatin1()?
when to use toUtf8()?heavily depends on your usecase and what you want to do with the string
-
for example?
-
now i have a MFC project that use CString. the project is using Unicode is VS propety, but i think i have to take care of Multi-Byte.
i am changing this MFC project into Qt project.
so i want to know the right way to convert between QString and CString.
thanks -
@opengpu said in transforming QString to microsoft's CString, and CString to QString:
for example?
For example if your string only contains Latin-1 characters you can use toLatin1(). It is described in the documentation: https://doc.qt.io/qt-5/qstring.html#toLatin1
Using Google I found: https://stackoverflow.com/questions/6215833/how-do-i-convert-an-atl-mfc-cstring-to-a-qstring
-
IIRC, when UNICODE is defined, the format of CString and QString is both UTF-16, so that should be the easiest format to exchange data.
https://doc.qt.io/qt-5/qstring.html#fromWCharArray and https://doc.qt.io/qt-5/qstring.html#toWCharArray should work for you.
You can probably also use https://doc.qt.io/qt-5/qstring.html#utf16 but remember that this will only work with casting and on Windows (where
wchar_t
is 16 bit).Regards