Convert a hexadecimal string to an integer efficiently in C?

后端 未结 16 2011
暖寄归人
暖寄归人 2020-12-01 09:31

In C, what is the most efficient way to convert a string of hex digits into a binary unsigned int or unsigned long?

For example, if I have

相关标签:
16条回答
  • 2020-12-01 10:10

    If you don't have the stdlib then you have to do it manually.

    unsigned long hex2int(char *a, unsigned int len)
    {
        int i;
        unsigned long val = 0;
    
        for(i=0;i<len;i++)
           if(a[i] <= 57)
            val += (a[i]-48)*(1<<(4*(len-1-i)));
           else
            val += (a[i]-55)*(1<<(4*(len-1-i)));
    
        return val;
    }
    

    Note: This code assumes uppercase A-F. It does not work if len is beyond your longest integer 32 or 64bits, and there is no error trapping for illegal hex characters.

    0 讨论(0)
  • 2020-12-01 10:11

    @Eric

    Why is a code solution that works getting voted down? Sure, it's ugly and might not be the fastest way to do it, but it's more instructive that saying "strtol" or "sscanf". If you try it yourself you will learn something about how things happen under the hood.

    I don't really think your solution should have been voted down, but my guess as to why it's happening is because it's less practical. The idea with voting is that the "best" answer will float to the top, and while your answer might be more instructive about what happens under the hood (or a way it might happen), it's definitely not the best way to parse hex numbers in a production system.

    Again, I don't think there's anything wrong with your answer from an educational standpoint, and I certainly wouldn't (and didn't) vote it down. Don't get discouraged and stop posting just because some people didn't like one of your answers. It happens.

    I doubt my answer makes you feel any better about yours being voted down, but I know it's especially not fun when you ask why something's being voted down and no one answers.

    0 讨论(0)
  • 2020-12-01 10:13

    Try this:

    #include <stdio.h>
    int main()
    {
        char s[] = "fffffffe";
        int x;
        sscanf(s, "%x", &x);
        printf("%u\n", x);
    }
    
    0 讨论(0)
  • 2020-12-01 10:13

    For AVR Microcontrollers I wrote the following function, including relevant comments to make it easy to understand:

    /**
     * hex2int
     * take a hex string and convert it to a 32bit number (max 8 hex digits)
     */
    uint32_t hex2int(char *hex) {
        uint32_t val = 0;
        while (*hex) {
            // get current character then increment
            char byte = *hex++; 
            // transform hex character to the 4bit equivalent number, using the ascii table indexes
            if (byte >= '0' && byte <= '9') byte = byte - '0';
            else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
            else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;    
            // shift 4 to make space for new digit, and add the 4 bits of the new digit 
            val = (val << 4) | (byte & 0xF);
        }
        return val;
    }
    

    Example:

    char *z ="82ABC1EF";
    uint32_t x = hex2int(z);
    printf("Number is [%X]\n", x);
    

    Will output:

    0 讨论(0)
提交回复
热议问题