Convert hex string (char []) to int?

前端 未结 13 1081
栀梦
栀梦 2020-11-22 12:12

I have a char[] that contains a value such as \"0x1800785\" but the function I want to give the value to requires an int, how can I convert this to an int? I have searched a

13条回答
  •  一个人的身影
    2020-11-22 12:51

    Something like this could be useful:

    char str[] = "0x1800785";
    int num;
    
    sscanf(str, "%x", &num);
    printf("0x%x %i\n", num, num); 
    

    Read man sscanf

提交回复
热议问题