Thanks for comments, Nico.

 I personally like the old know-it-all mode, because I've learned
a lot from it !


 Here's the reply for your comments.

  I think strings obtained with "fgets" end with "\n\0", right ?
In order to manipulate the output appearance, I replaced the '\n'
with '\0', so there are two '\0's at the end of the string "input".

Thanks,
Yutaka

----- Original Message ----
From: Nico Heinze <[EMAIL PROTECTED]>
To: [email protected]
Sent: Monday, April 16, 2007 8:57:19 AM
Subject: [c-prog] Re: Hexadecimal to Decimal

--- In [email protected], Yutaka OKAIE <[EMAIL PROTECTED]> wrote:
>
> 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
<snip>

Hi Yutaka,

almost there. The old know-it-all has only one thing to moan about,
and that's the (incorrect) position of the '\0' character that you set
at the end of the string to convert:
1) The position of the '\0' character is at 'strlen( input)', not at
'strlen( input) - 1'. Just take into account that the first element of
every array is stored at position 0, not 1.
2) Why do you set this '\0' at all? It is already there.

Besides this issue it's a far better code. Now, if you please, it
would be great to have useful variable names for _all_ variables
(including loop counters and index values) and some comments in the code.

Sorry, I didn't mean to diminish your work; I'm just once more in the
know-it-all mode. ;-)

Cheers,
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/
 

Reply via email to