Bytes to Binary in C

后端 未结 6 738
情书的邮戳
情书的邮戳 2021-02-04 14:22

I\'m trying to simply convert a byte received from fget into binary.

I know the value of the first byte was 49 based on printing the value. I now need to convert this in

6条回答
  •  忘了有多久
    2021-02-04 15:08

    #include
    #include 
    void main(void) {
        unsigned char byte = 49;// Read from file
        unsigned char mask = 1; // Bit mask
        unsigned char bits[8];
        int i, j = CHAR_BIT-1;
              // Extract the bits
        for ( i = 0; i < 8; i++,j--,mask = 1) {
        // Mask each bit in the byte and store it
        bits[i] =( byte & (mask<<=j))  != NULL;
        }
        // For debug purposes, lets print the received data
        for (int i = 0; i < 8; i++) {
           printf("%d", bits[i]);
       }
       puts("");
    }
    

提交回复
热议问题