Nico Heinze wrote:
> --- 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

While we are in "know-it-all" mode, BString has ConvertFromHex() and 
doesn't use strtol() because strtol() depends on the source to be zero 
terminated.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* VerifyMyPC 2.5
Change tracking and management tool.
Reduce tech. support times from 2 hours to 5 minutes.

http://www.CubicleSoft.com/VerifyMyPC/

Reply via email to