PDA

View Full Version : Converting QString to unsigned char



salston
24th April 2006, 21:33
Hi,

I am using QT 4.0 in a Linux (CentOS) environment. I need to convert from a QString to an unsigned char. However, I am having no luck. I tried the following:

Converting QString to "toStdString" and then typecasting to char. I received the following error: error: `struct std::string' used where a `char' was expected.

Any help would be appreciated. :confused:

high_flyer
24th April 2006, 21:42
From the docs:

You can also pass string literals to functions that take QStrings and the QString(const char *) constructor will be invoked. Similarily, you can pass a QString to a function that takes a const char * using the qPrintable() macro which returns the given QString as a const char *. This is equivalent to calling <QString>.toAscii().constData().

wysota
24th April 2006, 21:43
Solution 1:


QString str;
const char *c = str.toLocal8Bit().constData();

Solution 2:

QString str;
const char *c = qPrintable(str);

You should make a copy of that c variable if you want to store it as it is a temporary reference only.

JohnSuykerbuyk
24th April 2006, 22:10
Don't know if it helps, but this is a snippet from a utility file I wrote to do just what you want. It's just a couple of MACRO's, but lets you do explicit conversion to and from unicode, or automatic conversion depending on your project settings.

- John Suykerbuyk


#if defined(_WIN32)
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <QString>
/************************************************** *********
* Explicit QString to platform defined char* conversions and back
************************************************** *********/

/**
* Converts wide char* to QString.
* Example QString MyQString = WCHAR2QSTRING(const WCHAR* MyString)
**/
#define WCHAR2QSTRING QString::fromUtf16
/**
* Converts QString to wide char*.
* Example: const WCHAR* Str = MyQString.QSTRING2WCHAR()
**/
#define QSTRING2WCHAR(qStr) qStr.utf16()

/**
* Converts 8bit char* to QString.
* Example QString MyQString = NCHAR2QSTRING(const NCHAR* MyString)
**/
#define NCHAR2QSTRING QString::fromLocal8Bit

/**
* Converts QString to 8bit char*.
* Example: const NCHAR* Str = MyQString.QSTRING2NCHAR()
**/
#define QSTRING2NCHAR(qStr) qStr.toLocal8Bit()

/************************************************** *********
* Automatic QString to platform defined char* conversions and back
************************************************** *********/
#ifdef UNICODE
/*
* Converts wide char* to QString.
* Example QString MyQString = WCHAR2QSTRING(const WCHAR* MyString)
**/
#define CHAR2QSTRING QString::fromUtf16
/**
* Converts QString to wide char*.
* Example: const WCHAR* Str = MyQString.QSTRING2WCHAR()
**/
#define QSTRING2CHAR(qStr) qStr.utf16()
#else
/**
* Converts 8bit char* to QString.
* Example QString MyQString = NCHAR2QSTRING(const NCHAR* MyString)
**/
#define CHAR2QSTRING QString::fromLocal8Bit

/**
* Converts QString to 8bit char*.
* Example: const NCHAR* Str = MyQString.QSTRING2NCHAR()
**/
#define QSTRING2CHAR(qStr) qStr.toLocal8Bit()

#endif //#if defined UNICODE

#endif //#if defined(_WIN32)