Not sure where exactly but it does exist. I need to do a bunch of tests with it after installing Cygwin with g++ (GCC). For my own good I want to see the code, not just the result of function calls
I don't know if that meets your requirements. You still have not said what you're trying to do. Are you trying to convert from big-endian to little-endian?
How did you find out that there is such a function? "encode" is a very generic name so it's hard to know where it belongs and what kind of encoding it's doing.
I have 2 files test.h and test.cpp
I am supposed to write a bunch of code without touching void encode( char* buf, int size );
main(){} doesn't contain much and there is no custom implementation of encode(). There are only 3 files and so encode() comes from g++. Since I have char* I expect it to be for strings.
Try using grep -r "encode" . or grep -r "encode(" . or grep -r "void encode.*(" . or similar inside the directory where your compiler lives. If it's compiling and not crashing, the definition is somewhere.
Is this school work or something like that? Are you sure you understood the problem statement correctly? "Write an implementation of this function that matches this signature and passes these tests. Don't modify the signature" is quite a common problem in programming courses.
Yeah, you've misunderstood the instructions.
There is no "encode" in gcc. Now I think you're just being dishonest with that apparently fake comment.
And if you're not allowed to modify it, why do you need its implementation?
"Never attribute to malice that which can be adequately explained by stupidity."
I don't think he's lying, but rather it's ambiguity in the term "GCC static library". I do not think his instructor means that the code is part of the GCC C++ standard library+extensions.
@Oknel, your instructor most likely means the function is defined in a static library compatible with GCC. This is will most likely be called "lib*.a"
#include <iostream>
#include <string>
#include <vector>
#include <array>
staticconst std::string BASE64_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ""abcdefghijklmnopqrstuvwxyz""0123456789+/";
static std::string Base64Encode(const std::string& input)
{
std::string encoded;
unsigned val = 0;
int valb = -6;
for(unsignedchar c : input)
{
val = (val<<8) + c;
valb += 8;
while(valb >= 0)
{
encoded.push_back(BASE64_CHARS[ (val>>valb) & 0x3F ]);
valb-=6;
}
}
if (valb>-6)
encoded.push_back(BASE64_CHARS[ ((val<<8)>>(valb+8)) & 0x3F ]);
while(encoded.size() % 4)
encoded.push_back('=');
return encoded;
}
static std::string Base64Decode(const std::string& encoded_string)
{
std::string decoded;
// Prep the decode table
std::array<int, 256> table;
table.fill(-1);
for (int i=0; i<64; i++)
table[BASE64_CHARS[i]] = i;
unsigned val = 0;
int valb = -8;
for (unsignedchar c : encoded_string)
{
if (table[c] == -1)
break;
val = (val<<6) + table[c];
valb += 6;
if (valb>=0)
{
decoded.push_back(char((val>>valb) & 0xFF));
valb-=8;
}
}
return decoded;
}
int main()
{
usingnamespace std;
vector<string> v
{
"Hello!",
"Natural selection is anything but random.",
"Every beginning must have an end.",
"Call me Ishmael.",
"Cooper has scored 114 offenses against literary art out of a possible 115. It breaks the record."
};
string encoded, decoded;
for (string& orig : v)
{
cout << "original: " << orig << '\n';
encoded = Base64Encode(orig);
cout << "encoded: " << encoded << '\n';
decoded = Base64Decode(encoded);
cout << "decoded: " << decoded << '\n' << '\n';
}
return 0;
}
original: Hello!
encoded: SGVsbG8h
decoded: Hello!
original: Natural selection is anything but random.
encoded: TmF0dXJhbCBzZWxlY3Rpb24gaXMgYW55dGhpbmcgYnV0IHJhbmRvbS4=
decoded: Natural selection is anything but random.
original: Every beginning must have an end.
encoded: RXZlcnkgYmVnaW5uaW5nIG11c3QgaGF2ZSBhbiBlbmQu
decoded: Every beginning must have an end.
original: Call me Ishmael.
encoded: Q2FsbCBtZSBJc2htYWVsLg==
decoded: Call me Ishmael.
original: Cooper has scored 114 offenses against literary art out of a possible 115. It breaks the record.
encoded: Q29vcGVyIGhhcyBzY29yZWQgMTE0IG9mZmVuc2VzIGFnYWluc3QgbGl0ZXJhcnkgYXJ0IG91dCBvZiBhIHBvc3NpYmxlIDExNS4gSXQgYnJlYWtzIHRoZSByZWNvcmQu
decoded: Cooper has scored 114 offenses against literary art out of a possible 115. It breaks the record.
This can obviously be altered to accept a char* and a size.
As Ganado found, the Russian thread where OP also posted https://www.linux.org.ru/forum/development/14380125 . One joker says, "to not start another thread, I'll write here -- I need a woman and food, thx!" Another chimes in, "Me too, w/ some beer." And another says "Preferably where it's warm near the sea".
They basically all agreed it's not really present in the GCC source/extensions, and one guy even offered a basic XOR implementation strategy.