What's the fastest way to convert hex to integer in C++?

后端 未结 6 639
甜味超标
甜味超标 2021-02-01 08:52

I\'m trying to convert a hex char to integer as fast as possible.

This is only one line: int x = atoi(hex.c_str);

Is there a faster way

6条回答
  •  春和景丽
    2021-02-01 09:31

    This is my favorite hex-to-int code:

    inline int htoi(int x) {
        return 9 * (x >> 6) + (x & 017);
    }
    

    It is case-insensitive for letter, i.e will return correct result for "a" and "A".

提交回复
热议问题