|
-
September 28th, 2010, 11:27 AM
#1
Hex To Dec
Hi friends, could you please show me a quick and easy method to convert Hexadecimal to Decimal?
Something equivalent to the inverse function IntToHex( int value, int digits )
with the possibility to get a desired number of decimal digits.
Thx and regards
-
September 28th, 2010, 11:32 AM
#2
Re: Hex To Dec
Hex .. as in a string that contains a hex value ? You can use sscanf in combination with '%x' .
-
September 28th, 2010, 06:06 PM
#3
Re: Hex To Dec
Thanks, I have had to spend some time developing a function to do that.
It works fine, so here you are. Do not forget to call #include <math.h>
double HexToInt(String hexvalue)
{
double intvalue=0;
String singlechar;
String texttosplit = hexvalue;
int len=texttosplit.Length();
while (len>=1)
{
singlechar=texttosplit;
singlechar.SetLength(1);
if(singlechar=="0") { intvalue = intvalue + 0; }
if(singlechar=="1") { intvalue = intvalue + 1*pow(16,len-1); }
if(singlechar=="2") { intvalue = intvalue + 2*pow(16,len-1); }
if(singlechar=="3") { intvalue = intvalue + 3*pow(16,len-1); }
if(singlechar=="4") { intvalue = intvalue + 4*pow(16,len-1); }
if(singlechar=="5") { intvalue = intvalue + 5*pow(16,len-1); }
if(singlechar=="6") { intvalue = intvalue + 6*pow(16,len-1); }
if(singlechar=="7") { intvalue = intvalue + 7*pow(16,len-1); }
if(singlechar=="8") { intvalue = intvalue + 8*pow(16,len-1); }
if(singlechar=="9") { intvalue = intvalue + 9*pow(16,len-1); }
if(singlechar=="A" || singlechar=="a") { intvalue = intvalue + 10*pow(16,len-1); }
if(singlechar=="B" || singlechar=="b") { intvalue = intvalue + 11*pow(16,len-1); }
if(singlechar=="C" || singlechar=="c") { intvalue = intvalue + 12*pow(16,len-1); }
if(singlechar=="D" || singlechar=="d") { intvalue = intvalue + 13*pow(16,len-1); }
if(singlechar=="E" || singlechar=="e") { intvalue = intvalue + 14*pow(16,len-1); }
if(singlechar=="F" || singlechar=="f") { intvalue = intvalue + 15*pow(16,len-1); }
len--;
texttosplit.Delete(1,1);
}
return intvalue;
}
Best regards
Joaqu*n Gabás
-
September 28th, 2010, 08:19 PM
#4
Re: Hex To Dec
Jimmy gabas you can't loop ?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|