Convert a hexadecimal string to an integer efficiently in C?

后端 未结 16 2126
暖寄归人
暖寄归人 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

    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.

提交回复
热议问题