Problem with type casting?
Hi
I am new in QT.
I am trying to convert text string to hexadecimal.
Code:
void string2hex::browse()
{
res = String2Hex(str);
}
QString string2hex
:: Byte2Hex(unsigned char c
) {
//code which convert bytes into hex
}
{
unsigned int StrLen = S.length();
for (unsigned int i = 0; i < StrLen; i++)
Result += Byte2Hex((unsigned char)S[i]);
return Result;
}
I gives the compilation error
Quote:
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.
Re: Problem with type casting?
Have a look at the [] operator for a QString in the Qt documentation. It returns a const QChar instead of an unsigned char.
Try:
The toAscii() method will return a char which you need.
Re: Problem with type casting?
How about using QByteArray ? It already has QByteArray::toHex.
Also conversion between QString and QByteArray is a step away.