Click here to Skip to main content
Licence CPOL
First Posted 15 Feb 2006
Views 25,048
Bookmarked 14 times

FileToBin

By | 11 Jul 2006 | Article
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..!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Lampros Giampouras

Software Developer (Junior)

Greece Greece

Member

Physics Graduate
 
E-mail: labrosgiamp@yahoo.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalbut but... Pinmemberbakerfishsticks9:38 15 Feb '06  
GeneralRe: but but... PinmemberPJ Arends12:42 15 Feb '06  
Generalbut ... PinmemberMaximilien8:50 15 Feb '06  
GeneralRe: but ... PinmemberVertical_Horizon11:56 15 Feb '06  
GeneralRe: but ... PinmemberPJ Arends12:39 15 Feb '06  
GeneralRe: but ... PinmemberVertical_Horizon1:20 16 Feb '06  
Questionuse ? Pinmembertoxcct8:15 15 Feb '06  
AnswerRe: use ? PinmemberVertical_Horizon11:51 15 Feb '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 11 Jul 2006
Article Copyright 2006 by Lampros Giampouras
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid