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

  • Home
  • SEARCH
  • 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 9059763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:03:27+00:00 2026-06-16T15:03:27+00:00

This is my fourth attempt at doing base64 encoding. My first tries work but

  • 0

This is my fourth attempt at doing base64 encoding. My first tries work but it isn’t standard. It’s also extremely slow!!! I used vectors and push_back and erase a lot.

So I decided to re-write it and this is much much faster! Except that it loses data. -__-
I need as much speed as I can possibly get because I’m compressing a pixel buffer and base64 encoding the compressed string. I’m using ZLib. The images are 1366 x 768 so yeah.

I do not want to copy any code I find online because… Well, I like to write things myself and I don’t like worrying about copyright stuff or having to put a ton of credits from different sources all over my code..

Anyway, my code is as follows below. It’s very short and simple.

const static std::string Base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

inline bool IsBase64(std::uint8_t C)
{
    return (isalnum(C) || (C == '+') || (C == '/'));
}

std::string Copy(std::string Str, int FirstChar, int Count)
{
    if (FirstChar <= 0)
        FirstChar = 0;
    else
        FirstChar -= 1;
    return Str.substr(FirstChar, Count);
}

std::string DecToBinStr(int Num, int Padding)
{
    int Bin = 0, Pos = 1;
    std::stringstream SS;
    while (Num > 0)
    {
        Bin += (Num % 2) * Pos;
        Num /= 2;
        Pos *= 10;
    }
    SS.fill('0');
    SS.width(Padding);
    SS << Bin;
    return SS.str();
}

int DecToBinStr(std::string DecNumber)
{
    int Bin = 0, Pos = 1;
    int Dec = strtol(DecNumber.c_str(), NULL, 10);

    while (Dec > 0)
    {
        Bin += (Dec % 2) * Pos;
        Dec /= 2;
        Pos *= 10;
    }
    return Bin;
}

int BinToDecStr(std::string BinNumber)
{
    int Dec = 0;
    int Bin = strtol(BinNumber.c_str(), NULL, 10);

    for (int I = 0; Bin > 0; ++I)
    {
        if(Bin % 10 == 1)
        {
            Dec += (1 << I);
        }
        Bin /= 10;
    }
    return Dec;
}

std::string EncodeBase64(std::string Data)
{
    std::string Binary = std::string();
    std::string Result = std::string();

    for (std::size_t I = 0; I < Data.size(); ++I)
    {
        Binary += DecToBinStr(Data[I], 8);
    }

    for (std::size_t I = 0; I < Binary.size(); I += 6)
    {
        Result += Base64Chars[BinToDecStr(Copy(Binary, I, 6))];
        if (I == 0) ++I;
    }

    int PaddingAmount = ((-Result.size() * 3) & 3);
    for (int I = 0; I < PaddingAmount; ++I)
        Result += '=';

    return Result;
}

std::string DecodeBase64(std::string Data)
{
    std::string Binary = std::string();
    std::string Result = std::string();

    for (std::size_t I = Data.size(); I > 0; --I)
    {
        if (Data[I - 1] != '=')
        {
            std::string Characters = Copy(Data, 0, I);
            for (std::size_t J = 0; J < Characters.size(); ++J)
                Binary += DecToBinStr(Base64Chars.find(Characters[J]), 6);
            break;
        }
    }

    for (std::size_t I = 0; I < Binary.size(); I += 8)
    {
        Result += (char)BinToDecStr(Copy(Binary, I, 8));
        if (I == 0) ++I;
    }

    return Result;
}

I’ve been using the above like this:

int main()
{
    std::string Data = EncodeBase64("IMG." + ::ToString(677) + "*" + ::ToString(604));  //IMG.677*604
    std::cout<<DecodeBase64(Data);        //Prints IMG.677*601
}

As you can see in the above, it prints the wrong string. It’s fairly close but for some reason, the 4 is turned into a 1!

Now if I do:

int main()
{
    std::string Data = EncodeBase64("IMG." + ::ToString(1366) + "*" + ::ToString(768));  //IMG.1366*768
    std::cout<<DecodeBase64(Data);        //Prints IMG.1366*768
}

It prints correctly.. I’m not sure what is going on at all or where to begin looking.

Just in-case anyone is curious and want to see my other attempts (the slow ones): http://pastebin.com/Xcv03KwE

I’m really hoping someone could shed some light on speeding things up or at least figuring out what’s wrong with my code :l

  • 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-16T15:03:28+00:00Added an answer on June 16, 2026 at 3:03 pm

    The main encoding issue is that you are not accounting for data that is not a multiple of 6 bits. In this case, the final 4 you have is being converted into 0100 instead of 010000 because there are no more bits to read. You are supposed to pad with 0s.

    After changing your Copy like this, the final encoded character is Q, instead of the original E.

    std::string data = Str.substr(FirstChar, Count);
    while(data.size() < Count) data += '0';
    return data;
    

    Also, it appears that your logic for adding padding = is off because it is adding one too many = in this case.

    As far as comments on speed, I’d focus primarily on trying to reduce your usage of std::string. The way you are currently converting the data into a string with 0 and 1 is pretty inefficent considering that the source could be read directly with bitwise operators.

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

Sidebar

Related Questions

This is my lame first attempt. I would like to get a count of
Right now i have this ***FIRST***1DESIGNRESULTSM25Fe415 ***Second***Fe415 ***Third***1500.0mm ***Fourth***300.0mmX600.0mmCOVER:40.0mm ***Fifth***15ENDJOINT:13SHORTCOLUMN ***Sixth***5472.00Sq.mm.REQD.CONCRETEAREA:174528.00Sq.mm ***Seventh***12-25dia.(3.27%,5890.49Sq.mm.)(Equallydistributed) ***Eighth***8mm ***Ninth***300mmc/c
I am new to MVC so this is my first attempt and I am
I'm getting ready to attempt my first project involving networked communications. This is just
This is my first attempt to throw data back and forth between a local
This is pretty straight forward. EDIT: Updated question and added fourth echo. Here is
I have a folder structure like this: /articles .index.php .second.php .third.php .fourth.php If I'm
this is my first question in here, and I would like to ask if
This sequence satisfies a(n+2) = 2 a(n+1) + 2 a(n). and also a(n)=[(1+sqrt(3))^(n+2)-(1-sqrt(3))^(n+2)]/(4sqrt(3)). I
This script: <?php $lines[] = ''; $lines[] = 'first line '; $lines[] = 'second

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.