Results 1 to 4 of 4

Thread: Converting QString to unsigned char

  1. #1
    Join Date
    Apr 2006
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Converting QString to unsigned char

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Converting QString to unsigned char

    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().

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting QString to unsigned char

    Solution 1:

    Qt Code:
    1. QString str;
    2. const char *c = str.toLocal8Bit().constData();
    To copy to clipboard, switch view to plain text mode 

    Solution 2:
    Qt Code:
    1. QString str;
    2. const char *c = qPrintable(str);
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Feb 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting QString to unsigned char

    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)

Similar Threads

  1. convert unsigned char * to QString
    By sepehr in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 21:31
  2. QString to unsigned char *
    By darksaga in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2007, 08:52
  3. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 01:38
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  5. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 21:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.