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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

759 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now