1

I am trying to implement the htoi(s) [Dennis Ritchie chapter2 exercise 2-3 Q] which converts a string of hexadecimal digits, into its equivalent integer, can anyone help me out in the logic of this program, i dont need the code, i just need the logic for implementing it. I am not able to get the correct logic

2
  • 1
    Do you know how you would convert a string of decimal digits to an integer? Then replace 10 with 16 everywhere in that, and you only need to convert a single hexadecimal digit to an integer. Commented Nov 26, 2012 at 16:34
  • Simple: use strtol with a radix of 16! Of course, this is probably not the answer the book is looking for... Commented Nov 26, 2012 at 16:41

3 Answers 3

1

Let's take a step back:

How would you implement a function that accepts a single hex digit and returns it's decimal equivalent?

In other words, how would you write this function:

unsigned int hextodec(char c)
{
    ... your code here
}

Hint: for decimal digits what happens when you calculate c -'0'?

Once you have this function it should be fairly easy to use it to calculate the conversion of longer hex strings.

Hint: 0xF is 15 and 0x10 is 16

Sign up to request clarification or add additional context in comments.

Comments

0

Use the strtol function with base 16.

About this way:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int xstrtol16(const char *s)
{
    errno = 0;
    char *endp = NULL;
    int result = strtol(s, &endp, 16);

    if (*charp || errno) {
       fprintf(stderr, "Failed to convert...\n");
       exit(1);
    }

    return result;
}

2 Comments

I think the point is to write it oneself, since it's an exercise. (Otherwise, yes, that's how one should do it.)
Since this is an exercise, your suggestion seems slightly counterproductive. Sure you can do it that way, but doing it that way doesn't really teach you how things work. And it's important to understand that.
0

Think about what a printable number (in any base) is. Basically a list of symbols which represent simple numeric values (0...base-1), and associated "weights" of their digit positions. For decimal the symbols are "0123456789" and the weights are 1, 10 , 100, 1000...

You convert the printable number into "integer" by finding the corresponding numeric value of each symbol, multiplying it times the weight for that digit position, and summing those products together. There are tricks to doing this efficiently, but a straight-forward "dumb" implementation can get the job done perfectly well.

Hex is slightly complicated because there are two runs of symbols -- 0-9 and A-F -- and it's sometimes desired to accept both upper-case and lower-case letters. There are again tricks to handling this, but doing it in a straight-forward fashion (compare each character to the limits of each run and decide which one it fits) is perfectly fine (and more general than other approaches). You can also even use a index op, where you simply find the character's index into "0123456789ABCDEF" -- inefficient but simple.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.