Problem in writing binary data on txt file
Aug 16, 2013 at 2:05pm UTC
I a want to write a code to convert a string into binary data for that i wrote a code its working perfectly but there is one problem , some of the binary data is written in 7bit and i want to convert it to 8 bit by adding 0 to the last. i am giving the code please help me
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string StringToBin(string);
string IntToBin(int );
int main()
{
string line1;
//string line2[30];
ifstream myfile("MSG_in.txt" );
ofstream nyfile("MSG_out.txt" );
// int a = 0;
// int b = 0;
if (!myfile)
{
cout<<"Error opening output file" <<endl;
system("pause" );
return -1;
}
while (!myfile.eof())
{
getline(myfile,line1,'\n' );
cout<<line1<<" " ;
cout << "\n" <<" Binary code of data is: " <<StringToBin(line1) << endl << endl;
nyfile << StringToBin(line1) << endl << endl;
// getline(myfile,line2[b],' ');
// cout<<"2."<<line2[b]<<"\n";
}
system("pause" );
}
//Multiplies a given base number by exponent amount of times. Handles signed integers only.
static const int intPow(int base, int power) {
if (base == 0) return 0;
int product = 1;
while (power > 0) {
product *= base; power--;
}
while (power < 0) {
product /= base; power++;
}
return product;
}
//converts a string to binary char by char using the IntToBin function
string StringToBin(string input) {
string output;
for (int i = 0; i < input.length(); i++) {
output += IntToBin(input.at(i)) + "\n" ;
}
return output;
}
//Converts an integer to binary format stored in a vector string. Handles signed integers only.
string IntToBin(int integer) {
char bits = 1; //minimal storage needed
string binary; //storage for binary number
//determine how many bits the binary conversion will need
while (intPow(2, bits) <= integer) {
bits++;
}
//determine each bit for binary number
for (;bits >= 0; bits--) {
if (intPow(2, bits) <= integer) {
binary += "1" ;
integer -= intPow(2, bits);
}
else {
binary += "0" ;
}
}
return binary;
}
Thanks in advance !!
Ashutosh
Aug 16, 2013 at 3:14pm UTC
You might insert the following @ line 83:
1 2
if ( bits < 8 )
binary = std::string(8-bits, '0' );
Or, you might consider a different approach altogether. In C++ you're capable of manipulating bits, so why not manipulate them?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iostream>
#include <limits>
#include <string>
typedef unsigned char byte_type;
std::string toBinary(byte_type byte)
{
std::string result;
byte_type mask = 1 << (std::numeric_limits<byte_type>::digits - 1);
while (mask)
{
result += byte & mask ? '1' : '0' ;
mask >>= 1;
}
return result;
}
int main()
{
std::cout << toBinary(0) << '\n' ;
for (byte_type i = 0; i < std::numeric_limits<byte_type>::max(); ++i)
std::cout << toBinary(i+1) << '\n' ;
}
http://ideone.com/lRl34j
Aug 16, 2013 at 3:55pm UTC
Thanks for your reply ....its done !!
Thank you !!
Topic archived. No new replies allowed.