PDA

View Full Version : convert char to hex, please help.. [Solved]



cooper
15th July 2009, 23:57
hi everyone,

i got a string from anther computer through serial port. i need to decode the character into hex value.

for example:

i get string "ABC", i need to calculate 0x41 + 0x42 + 0x43

how can i get the value of "A" in hex or in dec?

thanks in advance

nish
16th July 2009, 02:58
QString s="ABC";
int len=s.length();
int sum=0;
QChar ch;
for(int i=0;i<len;i++)
{
ch= s.at(i);
sum+=ch.unicode();
}

cooper
16th July 2009, 03:44
QString s="ABC";
int len=s.length();
int sum=0;
QChar ch;
for(int i=0;i<len;i++)
{
ch= s.at(i);
sum+=ch.unicode();
}

thanks Mr Death, it works.
i did not realize that the unicode is a number in decimal format, i need to go back to school :(

nish
16th July 2009, 03:46
even i need to go back to school :)

zeFree
30th April 2013, 01:49
A better (or should I say a perfect) example is available at Nokia Developer Community Wiki (http://www.developer.nokia.com/Community/Wiki/Convert_hexadecimal_to_decimal_and_vice-versa_in_Qt).
It goes like this:


/* hexadecimal 0xED8788DC is equivavelent to decimal 3985082588 */
QString str = "0xED8788DC";
bool ok;
uint appId = str.toUInt(&ok,16); //appId contains 3985082588

/* decimal 3985082588 is equivalent to hex ED8788DC */
uint decimal = 3985082588;
QString hexadecimal;
haxadecimal.setNum(decimal,16); //now hexadecimal contains ED8788DC

I hope this will help.