?
Solved

unsigned char * -> bits

Posted on 2000-03-23
3
Medium Priority
?
1,370 Views
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 )

Thanks,
-Gomata
0
Comment
Question by:lakshmee
3 Comments
 
LVL 2

Accepted Solution

by:
homer99 earned 200 total points
ID: 2651630
lakshemee:

in C++

#include<iostream.h>
#include<iomanip.h>

void displayBits(unsigned);

int main()
{
unsigned x;
char temp1;
cin >> temp1;
displayBits(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

0
 
LVL 3

Expert Comment

by:marcjb
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 */


/* EXAMPLE */
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 */


0
 
LVL 3

Expert Comment

by:kulina
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!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 (http://en.wikipedia.org/wiki/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 recursion 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.
Suggested Courses

862 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