C : converting binary to decimal

后端 未结 2 1053
难免孤独
难免孤独 2021-01-23 08:05

Is there any dedicated function for converting the binary values to decimal values. such as (1111 to 15 ) , ( 0011 to 3 ) .

Thanks in Advance

2条回答
  •  [愿得一人]
    2021-01-23 08:27

    Parse it with strtol, then convert to a string with one of the printf functions. E.g.

    char binary[] = "111";
    long l = strtol(binary, 0, 2);
    char *s = malloc(sizeof binary);
    sprintf(s, "%ld\n", l);
    

    This allocates more space than needed.

提交回复
热议问题