How can i convert binary to char?
I want to convert binary to char.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <fstream>
#include <bitset>
using namespace std;
int main(){
ifstream file1;
ofstream file2;
file1.open("C:\\Exam1.txt",ifstream::binary);
file2.open("D:\\Exam2.txt",ofstream::binary);
long size;
file1.seekg(0,ifstream::end);
size= file1.tellg();
file1.seekg(0);
char *a;
a=new char[size];
file1.read(a,size);
for(int i=0;i<size;i++){
bitset<8>binarya(a[i]);
file2.write(&(char)binarya.to_ulong(),size);
}
system("pause");
return 0;
}
|
And i tried this.But it doesn't work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <fstream>
#include <iostream>
#include <bitset>
using namespace std;
int main(){
ifstream file1;
ofstream file2;
file1.open("C:\\Exam1.txt",ifstream::binary);
file2.open("D:\\Exam2.txt",ofstream::binary);
long size;
file1.seekg(0,ifstream::end);
size= file1.tellg();
file1.seekg(0);
char *a;
a=new char[size];
file1.read(a,size);
for(int i=0;i<size;i++){
bitset<8>binarya(a[i]);
file2.write(&binarya.to_string<char,char_traits<char>,allocator<char> >()[i],size);
}
system("pause");
return 0;
}
|
Try this
1 2 3 4 5
|
for(int i=0;i<size;i++){
bitset<8>binarya(a[i]);
string s = binarya.to_string();
file2.write(s.c_str(),s.size());
}
|
I solved my problem.Thank u for ur all helps.
Topic archived. No new replies allowed.