435,136 Members | 1,087 Online
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

 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<
6 Replies

 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 char b = ; string bin=""; for(int i=0; i<8; i++) {   bin = (b mod 2) + bin;   b=b/2; }   if you have a string of characters, just iterate through each character and repeat this for each one.... Jun 26 '07 #2

 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

 100+ P: 1,806 to convert back from the bit string above we can do something like Expand|Select|Wrap|Line Numbers char bin[] =  char val = 0; for(int i=0; i<8 i++) {   val = val*2;   val = val + bin[i]; }   Jun 26 '07 #4

 P: 16 to convert back from the bit string above we can do something like Expand|Select|Wrap|Line Numbers char bin[] =  char val = 0; for(int i=0; i<8 i++) {   val = val*2;   val = val + bin[i]; }   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

 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

 Expert Mod 5K+ P: 9,197 Don't overlook using the STL: Expand|Select|Wrap|Line Numbers int data = 11234; bitset<32> bits(data); string onesandzeroes = bits.to_string();   and you are done. Jun 26 '07 #7