Bytes to Binary in C
Asked Answered
H

7

9

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 into its binary value.

unsigned char byte = 49;// Read from file
unsigned char mask = 1; // Bit mask
unsigned char bits[8];

  // Extract the bits
for (int i = 0; i < 8; i++) {
    // Mask each bit in the byte and store it
    bits[i] = byte & (mask << i);
}
 // For debug purposes, lets print the received data
for (int i = 0; i < 8; i++) {
printf("Bit: %d\n",bits[i]);
}

This will print:

Bit: 1
Bit: 0
Bit: 0
Bit: 0
Bit: 16
Bit: 32
Bit: 0
Bit: 0
Press any key to continue . . .

Clearly, this is not a binary value. Any help?

Helicopter answered 5/11, 2009 at 19:36 Comment(0)
T
16

The problem you're having is that your assignment isn't resulting in a true or false value.

bits[i] = byte & (mask << i);

This gets the value of the bit. You need to see if the bit is on or off, like this:

bits[i] = (byte & (mask << i)) != 0;
Turbot answered 5/11, 2009 at 19:39 Comment(0)
O
7

Change

bits[i] = byte & (mask << i);

to

bits[i] = (byte >> i) & mask;

or

bits[i] = (byte >> i) & 1;

or

bits[i] = byte & 1;
byte >>= 1;
Outgo answered 5/11, 2009 at 19:40 Comment(0)
O
4

One way, among many:

#include <stdio.h>
#include <limits.h>

int main(void) {
    int i;
    char bits[CHAR_BIT + 1];
    unsigned char value = 47;

    for (i = CHAR_BIT - 1; i >= 0; i -= 1) {
        bits[i] = '0' + (value & 0x01);
        value >>= 1;
    }

    bits[CHAR_BIT] = 0;

    puts(bits);

    return 0;
}
Obsessive answered 5/11, 2009 at 19:38 Comment(0)
C
1

You may notice that your output has a couple 1's and 0's, but also powers of 2, such as 32. This is because after you isolate the bit you want using the mask, you still have to bit-shift it into the least-significant digit so that it shows up as a 1. Or you could use what other posts suggested, and instead of bit-shifting the result (something like 00001000 for example), you could simply use (result != 0) to fetch either a 1 or 0, since in C, false is 0, and comparisons such as != will return 1 as true (I think).

Computer answered 5/11, 2009 at 20:52 Comment(0)
H
0
#include<Stdio.h>
#include <limits.h>
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("");
}
Hardy answered 16/8, 2016 at 0:28 Comment(0)
S
0

If you're using a system with the GNU C Library, ver 2.35 or later, you can convert a byte to binary using the %b conversion specification as follows:

#include <stdio.h>

int main() {
    unsigned char byte = 49;
    printf("binary rep. of char byte 49: %#b \n", byte);
    printf("Enter a decimal value between 0 & 255:\n");
    scanf("%d", &byte);
    printf("binary rep of entered decimal value: %#b \n", byte);
}

Verify you've got a version of glibc > 2.34:

ldd --version

Compile:

gcc -o b-con b-con.c

Maybe someday, there will be a binary conversion for scanf also?

Supplementary answered 29/1, 2024 at 3:20 Comment(0)
P
-1

This addition in place of that will work:

bits[i]= byte & (mask << i); 
bits[i] >>=i;
Phagocytosis answered 28/6, 2012 at 10:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.