PDA

View Full Version : Problem with type casting?



Jyoti.verma
24th August 2009, 22:40
Hi

I am new in QT.
I am trying to convert text string to hexadecimal.



void string2hex::browse()
{
QString str="abcd";
QString res;
res = String2Hex(str);
QMessageBox::information(this,tr("Output is"),res);
}

QString string2hex:: Byte2Hex(unsigned char c)
{
//code which convert bytes into hex
}
QString string2hex:: String2Hex(QString S)
{
unsigned int StrLen = S.length();
QString Result;

for (unsigned int i = 0; i < StrLen; i++)
Result += Byte2Hex((unsigned char)S[i]);
return Result;
}

I gives the compilation error


string2hex.cpp: In member function ‘QString string2hex::String2Hex(QString)’:
string2hex.cpp:53: error: invalid cast from type ‘QCharRef’ to type ‘unsigned char’


So can anyone tell me how to do this type casting?

thanks in advance.

blukske
24th August 2009, 23:03
Have a look at the [] operator for a QString in the Qt documentation. It returns a const QChar instead of an unsigned char.

Try:



S[i].toAscii()


The toAscii() method will return a char which you need.

aamer4yu
25th August 2009, 05:24
How about using QByteArray ? It already has QByteArray::toHex.
Also conversion between QString and QByteArray is a step away.