PDA

View Full Version : convert unsigned char * to QString



sepehr
9th December 2008, 15:12
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?

caduel
9th December 2008, 15:34
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

rexi
9th December 2008, 15:34
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 ;)

sepehr
9th December 2008, 17:19
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?

caduel
9th December 2008, 20:31
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