PDA

View Full Version : converting the value from hexadecimal to decimal value



jjbabu
16th November 2007, 10:58
Hi to all,

i recieved hexadecimal value through the serialport,and i read that value and assingned to string like this,
QString str;

str=fields[0]; //(hex value like this '0F12')

now i want to convert this hexavalue to decimal value and that should be display on the line edit.

please assist me to overcome this,

thanks in advance.

^NyAw^
16th November 2007, 11:19
Hi,

You have to transform the Hex code to binary (make packs of 4 bits):
0F12 = 0000 1111 0001 0010
0 F 1 2

Then, you have a binary number that can be interpreted as decimal using base2:

0*2^15+0*2^14+0*2^13+0*2^12+1*2^11+1*2^10+1*2^9+1* 2^8+0*2^7+0*2^6+0* 2^5+1*2^4+0*2^3+0*2^2+1*2^1+0*2^0 = 3858

The bit X is multiplied by 2^(position of the bit)

jpn
16th November 2007, 11:24
QString::number()
QString::toInt()

Both take base as parameter.