I want to convert a string of 6 bytes in hexadecimal to a float
float desencapsularArchivo::hex2dec(string aConvertir)
{
int cantHex = aConvertir.size();
float decimal = 0;
for(int hexNum = 0;hexNum < cantHex; hexNum++)
{
char piv = aConvertir.at(hexNum);
float hex;
switch(piv)
{
case 'A': case 'a':
hex = 10;
break;
case 'B': case 'b':
hex = 11;
break;
case 'C': case 'c':
hex = 12;
break;
case 'D': case 'd':
hex = 13;
break;
case 'E': case 'e':
hex = 14;
break;
case 'F': case 'f':
hex = 15;
break;
default:
hex = (int)piv - 48;
}
decimal = decimal + hex * pow(16, cantHex - hexNum - 1);
}
return decimal;
}
If I want to enter for example the string 00805f181015
the result is:
551351222272
instead of:
551351226389
According to my tests, the last 4 characters of the hexadecimal are missing, but I do not know why it does not.
It is worth mentioning that to print use cout << "Decimal: " << setprecision(0) << fixed << decimal;