FYI & for my practice...
Here's the code that accommodates the comments of Nico's,
the old know-it-all :), and uses "array" instead of "switch".
Is this a complete sample for this problem?
----- <beginning of code> -----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BUF_SIZE 256
int
main(void)
{
int ii;
long ans;
size_t ll;
char input[MAX_BUF_SIZE];
char hex[] = "0123456789ABCDEFabcdef";
int dec[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15};
char *pp;
if (fgets(input, MAX_BUF_SIZE - 1, stdin) == NULL) {
fprintf(stderr, "[err] no data was read");
exit(EXIT_FAILURE);
}
ans = 0;
ll = strlen(input);
for (ii = 0; ii < ll - 1; ii++) {
pp = strchr(hex, input[ii]);
if (pp == NULL) {
fprintf(stderr, "[err] invalid input\n");
exit(EXIT_FAILURE);
}
else {
ans *= 16;
ans += dec[pp - hex];
}
}
input[ll - 1] = '\0';
printf("%s (hex) = %ld (dec)\n", input, ans);
return 0;
}
----- <end of code> -----
The version usign "strtol" might be something like this.
----- <beginning of code> -----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BUF_SIZE 256
int
main(void)
{
long ans;
char input[MAX_BUF_SIZE], tmp[MAX_BUF_SIZE + 2];
if (fgets(input, MAX_BUF_SIZE - 1, stdin) == NULL) {
fprintf(stderr, "[err] no data was read");
exit(EXIT_FAILURE);
}
sprintf(tmp, "0x%s", input);
ans = strtol(tmp, NULL, 0);
input[strlen(input) - 1] = '\0';
printf("%s (hex) = %ld (dec)\n", input, ans);
return 0;
}
----- <end of code> -----
Thanks,
Yutaka
----- Original Message ----
From: Nico Heinze <[EMAIL PROTECTED]>
To: [email protected]
Sent: Monday, April 16, 2007 1:22:50 AM
Subject: [c-prog] Re: Hexadecimal to Decimal
--- 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
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/c-prog/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/