Convert a hexadecimal string to an integer efficiently in C?

后端 未结 16 2221
暖寄归人
暖寄归人 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 09:55

    In C you can convert a hexadecimal number to decimal in many ways. One way is to cast the hexadecimal number to an integer. I personally found this to be simple and small.

    Here is an sample code for converting a Hexadecimal number to a Decimal number with the help of casting.

    #include 
    
    int main(){
        unsigned char Hexadecimal = 0x6D;   //example hex number
        int Decimal = 0;    //decimal number initialized to 0
    
    
            Decimal = (int) Hexadecimal;  //conversion
    
        printf("The decimal number is %d\n", Decimal);  //output
        return 0;
    }
    

提交回复
热议问题