Convert hex string (char []) to int?

前端 未结 13 943
栀梦
栀梦 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:56

    Try below block of code, its working for me.

    char *p = "0x820";
    uint16_t intVal;
    sscanf(p, "%x", &intVal);
    
    printf("value x: %x - %d", intVal, intVal);
    

    Output is:

    value x: 820 - 2080
    

提交回复
热议问题