std::string base64_encode(constchar *bytes_to_encode, unsignedint in_len){ std::string ret; int i = 0; int j = 0; unsignedchar char_array_3[3]; // store 3 byte of bytes_to_encode unsignedchar char_array_4[4]; // store encoded character to 4 bytes
while (in_len--) { char_array_3[i++] = *(bytes_to_encode++); // get three bytes (24 bits) if (i == 3) { // eg. we have 3 bytes as ( 0100 1101, 0110 0001, 0110 1110) --> (010011, 010110, 000101, 101110) char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; // get first 6 bits of first byte, char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); // get last 2 bits of first byte and first 4 bit of second byte char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); // get last 4 bits of second byte and first 2 bits of third byte char_array_4[3] = char_array_3[2] & 0x3f; // get last 6 bits of third byte
for (i = 0; (i < 4); i++) ret += base64_chars[char_array_4[i]]; i = 0; } }
if (i) { for (j = i; j < 3; j++) char_array_3[j] = '\0';