By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,087 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

byte to bit & bit to byte

mb29
P: 3
i have been working on this problem for last 2 days but i think i need someones help

so here's my problem . . .

i want to read a character from a file, convert that into a string of 0's n 1's; store them into an array binary[8] and write them to another file.

then read the second file which contained the binary code of the file an convert it back to its exact original form . . . . but how?

n one last question what does this mean a bit or a byte :

fstream cfile("abc.dat");
char O = ' 0 ';
cfile<<O-48;
cfile.close();

I m working on c++ 3.0 compiler
Jun 26 '07 #1
Share this Question
Share on Google+
6 Replies


DeMan
100+
P: 1,806
a bit literally stands for "binary digit" and is basically a single digit in binary (a 1 or a 0). A Byte (look up in wikipedia, there is some debate over the exact origins of the word) is a series of bits (these days, usually 8) which are grouped together to represent something (eg with 8 bits to a byte we have 2^8 different arrangements - 0 to 255, which can be represented by the ascii characters).

It is not totally clear to me what you want to do, but it sounds like you want to convert a character to it's bit string. A character is simply a one-byte integer, and can (mathematically) be treated as such.

One way you could get this value is (in pseudocode)
Expand|Select|Wrap|Line Numbers
  1. char b = <the caracter you want>;
  2. string bin="";
  3. for(int i=0; i<8; i++)
  4. {
  5.   bin = (b mod 2) + bin;
  6.   b=b/2;
  7. }
  8.  
if you have a string of characters, just iterate through each character and repeat this for each one....
Jun 26 '07 #2

mb29
P: 3
ok thank u for ur reply.
let me explain my question once again to you

i want to read a character from a file (fstream, xyz.ext), convert that into a string of 0's n 1's(either integers or char's anyone); store them into an array int binary[8] and write them to another file.

after that again read the binary[8] data from the file(abc.dat) which we wrote in the first step and again convert that back into the original file( that was xyz.ext)
Jun 26 '07 #3

DeMan
100+
P: 1,806
to convert back from the bit string above we can do something like

Expand|Select|Wrap|Line Numbers
  1. char bin[] = <the binary string from before>
  2. char val = 0;
  3. for(int i=0; i<8 i++)
  4. {
  5.   val = val*2;
  6.   val = val + bin[i];
  7. }
  8.  
Jun 26 '07 #4

pntkiran
P: 16
to convert back from the bit string above we can do something like

Expand|Select|Wrap|Line Numbers
  1. char bin[] = <the binary string from before>
  2. char val = 0;
  3. for(int i=0; i<8 i++)
  4. {
  5.   val = val*2;
  6.   val = val + bin[i];
  7. }
  8.  
You want to read character from file ..mean you read "c" , then u want to convert into binary. " 1100011 " and then want to store it into int binary[8] = { 1,1,0,0,0,1,1} ; and then want to write binary data into other file.

and then after u want to do reverse of that ...Wrong then correct me ..

.
Kiran Parmar
Jun 26 '07 #5

mb29
P: 3
You want to read character from file ..mean you read "c" , then u want to convert into binary. " 1100011 " and then want to store it into int binary[8] = { 1,1,0,0,0,1,1} ; and then want to write binary data into other file.

and then after u want to do reverse of that ...Wrong then correct me ..

.
Kiran Parmar
yes u are right. if u can help me then plsease tell me further step in better way
Jun 26 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
Don't overlook using the STL:

Expand|Select|Wrap|Line Numbers
  1. int data = 11234;
  2. bitset<32> bits(data);
  3. string onesandzeroes = bits.to_string();
  4.  
and you are done.
Jun 26 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.