Decode Body Text
Gmail body data may be encoded
When you fetch a message, Gmail may place body content under a field like:
payload.parts[].body.data
That data value is often base64url encoded. Base64url is similar to base64, but it uses URL-safe characters and may omit padding.
Your C++ program must decode it before it can treat the message body as normal text.
In Lesson 3, that decoded text becomes the input for buy/sell signal parsing.
```cpp
#include <iostream>
#include <string>
#include <vector>
std::string toBase64(std::string s) {
for (char& c : s) {
if (c == '-') c = '+';
if (c == '_') c = '/';
}
while (s.size() % 4 != 0) {
s += '=';
}
return s;
}
std::string base64Decode(const std::string& input) {
const std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::vector<int> table(256, -1);
for (int i = 0; i < static_cast<int>(alphabet.size()); ++i) {
table[static_cast<unsigned char>(alphabet[i])] = i;
}
std::string output;
int value = 0;
int bits = -8;
for (unsigned char c : input) {
if (c == '=') break;
if (table[c] == -1) continue;
value = (value << 6) + table[c];
bits += 6;
if (bits >= 0) {
output.push_back(char((value >> bits) & 0xFF));
bits -= 8;
}
}
return output;
}
int main() {
std::string gmailData = "VHJhZGluZyBhbGVydDogQlVZIE1TRlQ";
std::string normalBase64 = toBase64(gmailData);
std::string decoded = base64Decode(normalBase64);
std::cout << "Encoded Gmail body: " << gmailData << "\n";
std::cout << "Decoded text: " << decoded << "\n";
return 0;
}
```



