--- In [email protected], "malaram kumhar" <[EMAIL PROTECTED]> wrote:
>
> Hexadecimal to Decimal
> 
> void Hex2Dec()
> {
>     char hex[N];
>     int i,j,n[N],l;
>     long double dec=0;
>     printf("Enter the hexadecimal number and find it's decimal 
> equivalent\n");
>     gets(hex);
>     l=strlen(hex);
>     for(i=0;i<l;i++)
>     {
>         switch(hex[i])
>         {
>         case '0':
>             n[i]=hex[i]-48;     
>             break;
>         case '1':
>             n[i]=hex[i]-48; 
>             break;
>         case '2':
>             n[i]=hex[i]-48;  
>             break;
>         case '3':
>             n[i]=hex[i]-48;  
>             break;
>         case '4':
>             n[i]=hex[i]-48;  
>             break;
>         case '5':
>             n[i]=hex[i]-48;  
>             break;
>         case '6':
>             n[i]=hex[i]-48;  
>             break;
>         case '7':
>             n[i]=hex[i]-48;  
>             break;
>         case '8':
>             n[i]=hex[i]-48;  
>             break;
>         case '9':
>             n[i]=hex[i]-48;  
>             break;
>         case 'A':
>             n[i]=hex[i]-55;     
> 
>             break;
>         case 'B':
>             n[i]=hex[i]-55;  
>             break;
>         case 'C':
>             n[i]=hex[i]-55;  
>             break;
>         case 'D':
>             n[i]=hex[i]-55;  
>             break;
>         case 'E':
>             n[i]=hex[i]-55;  
>             break;
>         case 'F':
>             n[i]=hex[i]-55;  
>             break;
>         }
>     }
>     for(i=0,j=l;i<l;i++,j--)
>         dec=dec+(n[j-1]*pow(16,i));
>     printf("The decimal equivalent is %lg \n",dec);
> }

Sorry, but this is a wonderful example of how NOT to perform such a
conversion operation. Why? Here are but a few points why this piece of
code is, to say at least, dangerous and less than optimal:

- Documentation? Comments? Meaningful names of variables?
  All of these are missing.
- The "gets()" function is dangerous because it does
  in no way check for buffer overflows.
- "strlen()" does not return an int; it returns a size_t
  in every compiler adherent to standards.
- The structure of the "switch" statement is awful; a far cleaner
  way to do the same with a "switch" is:
    switch( hex[i])
    { case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
        n [i] = hex [i] - '0';
        break;

      case 'A':
      case 'B':
      case 'C':
      case 'D':
      case 'E':
      case 'F':
        n [i] = hex [i] - 'A';
        break;

      case 'a':
      case 'b':
      case 'c':
      case 'd':
      case 'e':
      case 'f':
        n [i] = hex [i] - 'a';
        break;

    }
- There is no "default" case.
- Using the constants 48 and 55 is unwise as they are
  by far not self-explanatory and fit only to ASCII.
  What about IBM mainframes which use EBCDIC?
- This code snippet cannot be compiled on its own
  due to the undeclared constant(?) "N".
- There is no error checking, check for buffer or
  array overflows, and the like. This is nothing bad
  in itself for such a demonstration program, but then
  you should at least have mentioned that this is an
  incomplete sample and what you have left out here.


A far easier way to perform this conversion is "strtol()". Just make
sure that the string to be converted starts with the two characters
"0x", then "strtol()" will automatically convert from hex to decimal
(provided that you set the third parameter to "strtol()" to 0).
This will also get rid of the construct with array "n" and the use of
the function "pow()" which is total overkill here.


Regards,
the old know-it-all Nico

Reply via email to