unsigned char * -> bits

Posted on 2000-03-23
Last Modified: 2011-09-20

I have a  file which contains unsigned char stream . I wish to extract/convert that into
bit stream (0s and 1s )

Could you please suggest an efficient way to do that please  !! ( Suggestion in C++ is also fine )

Question by:lakshmee

Accepted Solution

homer99 earned 50 total points
ID: 2651630

in C++


void displayBits(unsigned);

int main()
unsigned x;
char temp1;
cin >> temp1;
return 0;

void displayBits(unsigned value)
unsigned c, displayMask = 1 << 15;

cout<<setw(7)<<value<< " = ";

for(c = 1; c<=16; c++) {
  cout << (value & displayMask ? '1' : '0' );
value <<= 1;

if(c%8 == 0)
  cout << ' ';

in this example a char is read in, the program then performs a bitwise and with 1, which does not affect the char and then the bits are output to the screen.  This is just an example, but it should work.
Let me know


Expert Comment

ID: 2653634
Read in a buffered amount of data (I suggest about 4K at a time to reduce disk access), and then cycle though the data converting the character to there 8-bit string representation.  Do with the conveted string whatever you need.

An old function I had called num2BinString takes a number (or a character), and converts it to the binary representation of the number.

Pass a string to hold the answer (temp in the example), the data to convert (the variable 'a' in the example, and the number of bits to use (for characters, use 8).  Remember that the number of bits to use must be less than the size of the array you are passing in.  For example, if you wanted the binary representation of 16-bit integers, the temp variable would need to be 17 characters in length.

#include <stdio.h>

char* num2BinString(char* s, long n, int c)
unsigned long mask = 1 << (c-1);
int ctr;

for( ctr = 0; ctr < c; ctr++, mask = mask >> 1 )  /* mask/=2 */
    *(s+ctr) = ((n & mask) > 0) ? '1' : '0';
*(s+ctr) = 0; /* NULL TERMINATE */
return s;
} /* num2BinString */

int main()
unsigned char a = 'a';
char temp[9];
num2BinString(temp, (long)a, 8); /* Want 8 bits */
printf("The letter %c is %s\n", a, temp);
return 0;
} /* main */


Expert Comment

ID: 2660225
Here is my solution in C:

#include <stdio.h>

int main(){
    char ch;
    unsigned int x;
    x = 1;
    printf("Enter char to convert to binary: ");
    scanf("%c", &ch);
    x <<= (sizeof(int)*8-1);
    printf("Char %c in binary is: \n", ch);
    while( x ){
        putchar(x & ch ? '1' : '0');
        x >>= 1;
    printf ("\n\n");
    return 0;

Now if you want you could add some error checking or some loop process your stream.
Good Luck!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question