Convert file to base64? (confused)

I'm looking to convert audio, images, video to base64 in c++, and through many searches on the web I've only seem to come up with string > base64.

When I view a .mp3 file for example, there are many characters there that are not available to be used in my string to encode.

http://www.adp-gmbh.ch/cpp/common/base64.html
I have this code set up in my project, and all I want is to be able to give the location of any file and have it encode (could just encode to a string, I can handle writing it to a file), and then give a location to put the decoded file as well.

I'm not sure exactly where to even start.
Why do you want to use base64??

jonah876 wrote:
through many searches on the web I've only seem to come up with string > base64.
A file is just a string with characters you might not be able to type on your keyboard. The C++ std::string class can easily hold any file, even with null characters.
I'm just having trouble turning the file into a string.


I've tried this, just to test if I can get a file in and spit it back out, but the resulting file looks extremely similar in notepad, but doesn't work in VLC, Photo viewer etc. when renamed.
1
2
3
4
5
6
7
8
9
10
    string line;
    ifstream myfile ("ob.jpg", ios::in | ios::binary);
    ofstream output ("example.txt");
    if (myfile.is_open())
    {
        while(getline(myfile,line)){
        output << line;
        }
        myfile.close();
    }
Last edited on
It appears as though the output is very similar however still different than the input.

It mostly seems to be a problem with spacing and lines, I'm not sure why that code would mess up the spacing on a file...

First Lines of Input:
ID3    T TXXX     duration 289 TXXX   
  starttime 0 TXXX     totalduration 289 TXXX     width 640 TXXX     height 480 TXXX     videodatarate 593 TXXX     audiodatarate 129 TXXX     totaldatarate 730 TXXX     framerate 30 TXXX   &  bytelength 26385123 TXXX     canseekontime true TXXX   (  sourcedata BADC21404HH1317682476704832 TXXX     purl  TXXX     pmsg  TSSE   
  Lavf52.87.1 COMM   h   engiTunNORM  000017F0 00001691 00014B5C 0001494A 0000F8B5 0000F8B5 00008000 00008000 00000D83 00000D83 


First Lines of Output:
ID3    T TXXX     duration 289 TXXX   
  starttime 0 TXXX     totalduration 289 TXXX     width 640 TXXX     height 480 TXXX     videodatarate 593 TXXX     audiodatarate 129 TXXX     totaldatarate 730 TXXX     framerate 30 TXXX   &  bytelength 26385123 TXXX     canseekontime true TXXX   (  sourcedata BADC21404HH1317682476704832 TXXX     purl  TXXX     pmsg  TSSE   
  Lavf52.87.1 COMM   h   engiTunNORM  000017F0 00001691 00014B5C 0001494A 0000F8B5 0000F8B5 00008000 00008000 00000D83 00000D83
Just taking a quick look at the code, I'd say if you are trying to make an exact duplicate of the file into example.txt, you'll need to include newlines as getline() extracts those from the original file but doesn't store them in the string.
Better use read() and write() stream member functions when you work in binary mode. getline and operators << and >> are not designed for that.
Thank you so much modoran! I looked up the read and write functions and this document helped me a lot:
http://www.cplusplus.com/reference/ostream/ostream/write/

Thanks to everyone that replied!
Topic archived. No new replies allowed.