Results 1 to 5 of 5

Thread: convert unsigned char * to QString

  1. #1
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default convert unsigned char * to QString

    hey guys,
    I'm looking for a way to convert unsigned char * to QString and vica versa ,I have used couple of methods I found while googling ,but some characters change upon conversion,
    forexample ╟ character would be sth else when converted from unsigned char,I think it might be the differente values of different character sets but no luck getting it to work,
    any ideas how to do the conversion?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: convert unsigned char * to QString

    unsigned char* and encodings are two different things.

    You can convert that by calling QString::fromAscii() or some similar function.
    (If need be cast the unsigned char* to char*.)

    The more important thing is that you have to know and (tell Qt) about the encoding of those bytes. See QTextCodec::setCodecForCStrings().

    HTH

  3. #3
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert unsigned char * to QString

    Have you tried the to/fromLocal8Bit() methods from QString?

    It might be helpful if you told us what you have tried so far that didn't work out

  4. #4
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert unsigned char * to QString

    I encode a string using Blowfish algorithm and it saves the encoded string as an unsigned char *,and now I have to convert it to QString, i tried the following and it works fine:
    //encodedPass is unsigned char *
    QString tmp=QString::fromLocal8Bit((char*)encodedPass);
    qDebug()<<tmp;
    and tmp is exactly what encodedPass is,but the problem is that when I want to show tmp using QMessageBox or put it in a QLineEdit it changes to another string!
    forexample:
    tmp is " ╖îf§' " and when I try to put it in a textbox using
    edt_userName->setText(tmp);
    " ·Œfªá' " will appear in textbox! why is that?

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: convert unsigned char * to QString

    The resulting "string" is not really a string but just an array of bytes.
    It probably contains stuff like 0-bytes and non-printable chars.

    To make it short: cryptographic hashes etc are not meant to be displayed as strings. Encode it to base64 or so and display that.

    HTH

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. convert from qstring to const char *
    By deepa.selvaraj in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2007, 12:33
  3. QString to unsigned char *
    By darksaga in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2007, 07:52
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  5. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20: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.