Integer.parseInt(s, 16) (Java) in C

Use sscanf function, like sscanf(s, "%4x", &x).

#include <stdio.h>

void main(){
    int x = 0;
    int ret;

    ret = sscanf("1f", "%4x", &x);
    printf("ret: %d\n", ret);        /* ret: 1 */
    if (ret > 0) {
        printf("success: %d\n", x);  /* success: 31 */
    }

    ret = sscanf("xxxx", "%4x", &x);
    printf("ret: %d\n", ret);        /* ret: 0 */
}


你可能感兴趣的:(Integer.parseInt(s, 16) (Java) in C)