FileToBin






1.57/5 (14 votes)
Converts any file to an array of bits.
Introduction
This is a class that converts any selected file from bytes to an array of bits and the other way around. I've seen this code elsewhere that I cannot remember, but I think that it wasn't totally right. So consider this code as an upgrade of something else.
The code
The class name is CFileToBinApp
, and has two member functions:
bool CFileToBinApp::Convert(CString file, int *binary) bool CFileToBinApp::Restore(int *binarray, int bytelength, unsigned char *bytes)
The file
is the file path (or filename) of the desired file, and the binary
variable points to the target array that will carry the bits.
For opening the file, the MFC CFile
is used.
bool CFileToBinApp::Convert(CString file, int *binary) { CFile f; f.Open(file,CFile::modeRead|CFile::typeBinary); int length=f.GetLength(); //table holding the bytes of the file unsigned char *bytes=new unsigned char[length]; f.Read(bytes,length); binary=new int[length*8]; //this will hold the binary table
A lookup table is needed in order to convert the bytes. A byte is from 0 to 255.
CString bin[256]; //look up table bin[0]="00000000"; bin[1]="00000001"; bin[2]="00000010"; bin[3]="00000011"; bin[4]="00000100"; bin[5]="00000101"; bin[6]="00000110"; bin[7]="00000111"; bin[8]="00001000"; bin[9]="00001001"; bin[10]="00001010"; bin[11]="00001011"; bin[12]="00001100"; . . . . . . . bin[253]="11111101"; bin[254]="11111110"; bin[255]="11111111";
So the conversion would be:
CString temp; //temporary string int a=0; //counter for (int i=0;i<length;i++) { temp=bin[bytes[i]]; binary[a] =temp[0]; //MSB binary[a+1]=temp[1]; // . binary[a+2]=temp[2]; // . binary[a+3]=temp[3]; // . binary[a+4]=temp[4]; // . binary[a+5]=temp[5]; // . binary[a+6]=temp[6]; // . binary[a+7]=temp[7]; //LSB a=a+8; }
You would say that the binary
array holds the binary data. But that's not the case.
What is really going on is that the binary
array holds the integer numbers of the characters "0" and "1". These numbers are "48" and "49" as shown below:
So in order to convert it to "0"s and "1"s, I subtract 48 from any value in the array.
for (int h=0;h<a;h++) { binary[h]-=48; } delete [] bytes; f.Close(); return true; }
The file is converted..!
To restore the bytes as they were, you must call the Restore()
function.
This function needs two types of information. The binary array that holds the bits to be restored, and the byte length of the original file. The restored bytes will be saved in a new array (bytes
) which will be created.
So the code looks like this:
bool CFileToBinApp::Restore(int *binarray, int bytelength, unsigned char *bytes) { int p=0; bytes=new unsigned char[bytelength]; for (int j=0;j<bytelength*8;j=j+8) { bytes[p]=128*binarray[j]+64*binarray[j+1]+32*binarray[j+2]+16* binarray[j+3]+8*binarray[j+4]+4*binarray[j+5]+2* binarray[j+6]+1*binarray[j+7]; p++; } return true; }
Why make such a class? I haven't yet seen a Microsoft class that takes the bits from a file, only the bytes. I hope that it will be as handy for you as it was for me..!