Click here to Skip to main content
15,908,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this is a very basic question but I'm newbie in Qt and I don't know to do it. I'm trying to convert a QString value (like "FF") to hexadecimal. I would like to obtain 0xFF. I am looking something like that

Source QString str= "20FF";
Target Result int hex= 0x20FF;

Could you help me? thanks.
Posted
Updated 18-Sep-19 18:56pm

QFile file("data.dat");
QByteArray Hex;
if (!file.open(QIODevice::ReadOnly))
{
;
}

Hex = file.readAll();
unsigned char hex[262182];
qint32 start = 0, size = 262182;
::memcpy(hex + start,Hex.constData(), size);
 
Share this answer
 
Comments
Richard Deeming 19-Sep-19 12:33pm    
An unexplained, unformatted code-dump is not a solution.
There is NO 'convert to Hexadecimal', actually.
str is the hexadecimal representation of the 8447 number.
0x20FF is just the literal[^] you could use in order to specify the number in the source code.

[update]
In order to compare the number represented by the string str with an actual int value, you have first to convert str to integer, using 16 as base. See "int QString::toInt(bool * ok = 0, int base = 10) const"[^]
[/update]
 
Share this answer
 
v2
Comments
Ramiien 9-Dec-15 6:16am    
Problem i am facing is I want to compare two values. Value from Network is in QString str= 20FF. and value in QHash is int value= 20FF.
CPallini 9-Dec-15 6:24am    
Have a look at my updated solution.
CPallini 9-Dec-15 7:05am    
QString provides the toLongLong method.
Jochen Arndt 9-Dec-15 7:05am    
For 64-bit values use QString::toULongLong(NULL, 16) or QString::toULongLong(NULL, 0) when your string begins with "0x".
See http://doc.qt.io/qt-5/qstring.html#toULongLong.

But note that the QString::toXXX functions fail when there are other characters (not only the plain number string). For these cases you may also use the standard C function strtoull() passing str.ToLocal8Bit().data().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900