CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Hex To Dec

  1. #1
    Join Date
    May 2008
    Posts
    44

    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

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Hex To Dec

    Hex .. as in a string that contains a hex value ? You can use sscanf in combination with '%x' .

  3. #3
    Join Date
    May 2008
    Posts
    44

    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

  4. #4
    Join Date
    Jul 2010
    Posts
    31

    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
  •  





Click Here to Expand Forum to Full Width

Featured