C++ how to calculate the decimal value of char (considered to be hex)

Viewed 29

I have an array of char:

char  in[5] = "401D";

I consider this to be a Hex-Number. Now I want to get the decimal value of this:

(4 * 16^3 + 0 * 16^2 + 1 * 16^1 + D * 16^0 = 16413)   ( where D= 13)

I did that like this but I am sure there would be better ways. Does anybody have a better idea?

      int   offset = 0;
      int   result = 0;

      for (int j = 0; j < 4; j++) 
      { 
        if (in[j] > 57)  
           offset = 7;
        result = result + (int) ( (in[j] - 48-offset) * pow(16, 3 - j) );
      }
0 Answers
Related