Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8535139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:21:49+00:00 2026-06-11T10:21:49+00:00

I wrote this little converter to convert Base2 file data to Base64. The resulting

  • 0

I wrote this little converter to convert Base2 file data to Base64. The resulting output is incorrect and I can’t figure out why. The problem is compounded if an image file is passed in as input: random NULL chars (0x00) are inserted into the output which should never happen. The output should only contain a subset of the Base64Table (see code).


Input:

Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.


Expected Output:

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4= 

(from Wikipedia page on Base64)


Actual Output:

YW7MaXPMZGnMdGnMZ3XMc2jMZCzMbm/MIG/MbHnMYnnMaGnMIHLMYXPMbizMYnXMIGLMIHTMaXPMc2nMZ3XMYXLMcGHMc2nMbiDMcm/MIG/MaGXMIGHMaW3MbHPMIHfMaWPMIGnMIGHMbHXMdCDMZiDMaGXMbWnMZCzMdGjMdCDMeSDMIHDMcnPMdmXMYW7MZSDMZiDMZWzMZ2jMIGnMIHTMZSDMb27MaW7MZWTMYW7MIGnMZGXMYXTMZ2HMbGXMZ2XMZXLMdGnMbiDMZiDMbm/MbGXMZ2XMIGXMY2XMZHPMdGjMIHPMb3LMIHbMaGXMZW7MZSDMZiDMbnnMY2HMbmHMIHDMZWHMdXLMLnLM

Code:

#include <iostream>
#include <fstream>

char Base64Table[64] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', //0-25
                        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', //26-51
                        '0','1','2','3','4','5','6','7','8','9',                                                                 //52-61
                        '+','/'};                                                                                                //62-63
char PADDING_CHAR = '=';

char GetFirstSymbol(char* buffer);
char GetSecondSymbol(char* buffer);
char GetThirdSymbol(char* buffer);
char GetFourthSymbol(char* buffer);

int main(int argc, char** argv) {

    if(argc != 2) {
        std::cout << "Incorrect number of arguments." << std::endl;
        std::cout << "Press Enter to quit.";
        while(!std::cin.get());
        return 0;
    }

    std::cout << "Converting " << argv[1] << std::endl;

    std::ifstream input;
    input.open(argv[1], std::ios_base::binary);

    std::ofstream output;
    output.open("data.base64", std::ios_base::binary);

    char count[1] = {'\0'};
    unsigned long file_size = 0;
    char buffer[3] = {'\0', '\0', '\0'};
    while(input.fail() == false) {
        input.read(reinterpret_cast<char*>(count), sizeof(count));
        ++file_size;
    }
    input.clear();
    input.seekg(0);

    while(input.fail() == false) {
        input.read(reinterpret_cast<char*>(buffer), sizeof(buffer));
        char firstsymbol = GetFirstSymbol(buffer);
        char secondsymbol = GetSecondSymbol(buffer);
        char thirdsymbol = GetThirdSymbol(buffer);
        char fourthsymbol = GetFourthSymbol(buffer);

        output.write(reinterpret_cast<char*>(&firstsymbol), sizeof(firstsymbol));
        output.write(reinterpret_cast<char*>(&secondsymbol), sizeof(secondsymbol));
        if(file_size % 3 == 2) {
            output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
            continue;
        } else if(file_size % 3 == 1) {
            output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
            output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
            continue;
        }
        output.write(reinterpret_cast<char*>(&thirdsymbol), sizeof(thirdsymbol));
        output.write(reinterpret_cast<char*>(&fourthsymbol), sizeof(fourthsymbol));
    }
    input.clear();
    input.close();

    output.clear();
    output.close();

    return 0;
}

//Gets the 6 most significant digits of the first byte.
char GetFirstSymbol(char* buffer) {
    int index = (buffer[1] >> 2);
    return Base64Table[index];
}

//Gets the 2 least significant digits from previous (first) byte and 4 most significant from the second byte.
char GetSecondSymbol(char* buffer) {
    int index = (((buffer[1] & 0x03) << 4) | ((buffer[2] & 0xF0) >> 4));
    return Base64Table[index];
}

//Gets the 4 least significant digits from previous (second) byte and 2 least significant from the third byte.
char GetThirdSymbol(char* buffer) {
    int index = (((buffer[2] & 0x0F) << 2) | ((buffer[3] & 0xC0) >> 6));
    return Base64Table[index];
}

//Gets the 6 least significant digits from the third byte.
char GetFourthSymbol(char* buffer) {
    int index = (buffer[3] & 0x3F);
    return Base64Table[index];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:21:51+00:00Added an answer on June 11, 2026 at 10:21 am

    C/C++ pointers and arrays are very powerful beasts but they can hurt you too! so you should take care when working with them. you define buffer as char[3] but access it from index [1] to index [3] and this is your error since indexes of C/C++ arrays are 0 based and according to this you have buffer [0][1][2][memory of other variables] you left buffer[0] unchanged and instead of that you are writing to the memory of another variable and this is the reason of random errors and invalid values!!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this little function to fill a drop down list with data from
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I wrote this code for zoom in/out . it works but even with one
I wrote this code for zoom in / out and it suppesed to only
I have this little script that generate a table in a .pdf file, but
i came across a little quest i am trying to convert this link that
I'm trying to figure out how to convert a jpg into a list of
I am a little confused on the logic of how to write this SQL
I wrote this as a simple dice game. It works as I want except
I wrote this short program which has a tiny GUI. Its supposed to allow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.