Bytes zu Binär in C

Ich versuche einfach, ein von fget empfangenes Byte in eine Binärdatei umzuwandeln.

Ich weiß, dass der Wert des ersten Bytes 49 war, basierend auf dem Drucken des Werts. Ich muss das jetzt in seinen Binärwert umwandeln.

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]);
}

Dies wird drucken:

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

Dies ist natürlich kein Binärwert. Irgendeine Hilfe?

Antworten auf die Frage(6)

Ihre Antwort auf die Frage