Convert from a binary to char in C

后端 未结 3 748
臣服心动
臣服心动 2020-12-16 18:37

I\'m flummoxed by how to convert from a binary value to a char in c.

For example, let\'s say I have 01010110 and want to print the corresponding letter \'V\' from t

3条回答
  •  独厮守ぢ
    2020-12-16 19:25

    #include 
    #include 
    int main(void)
    {
        char *data = "01010110";
        char c = strtol(data, 0, 2);
        printf("%s = %c = %d = 0x%.2X\n", data, c, c, c);
        return(0);
    }
    

    Output:

    01010110 = V = 86 = 0x56
    

    References:

    • strtol()
    • Correct usage of strtol()

提交回复
热议问题