को बढ़ावा देने के दुर्भाग्य से दो iterator_adaptors
binary_from_base64
के संयोजन और मिल transform_width
एक पूर्ण बेस 64 एन्कोडर/डिकोडर नहीं है। बेस 64 24 बिट्स (3 बाइट्स) के समूहों को 4 अक्षरों के रूप में दर्शाता है, जिनमें से प्रत्येक 6 बिट्स को एन्कोड करता है। यदि इनपुट डेटा ऐसे 3 बाइट समूहों का पूर्णांक नहीं है तो इसे एक या दो शून्य बाइट्स के साथ गद्देदार होना चाहिए। यह इंगित करने के लिए कि कितने पैडिंग बाइट जोड़े गए थे, एक या दो =
वर्ण एन्कोडेड स्ट्रिंग में जोड़े गए हैं।
transform_width
, जो 8 बिट बाइनरी के लिए 6 बिट पूर्णांक रूपांतरण के लिए ज़िम्मेदार है, यह पैडिंग स्वचालित रूप से लागू नहीं होता है, यह उपयोगकर्ता द्वारा किया जाता है। एक साधारण उदाहरण:
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
#include <iostream>
#include <string>
using namespace boost::archive::iterators;
using namespace std;
int main(int argc, char **argv) {
typedef transform_width< binary_from_base64<remove_whitespace<string::const_iterator> >, 8, 6 > it_binary_t;
typedef insert_linebreaks<base64_from_binary<transform_width<string::const_iterator,6,8> >, 72 > it_base64_t;
string s;
getline(cin, s, '\n');
cout << "Your string is: '"<<s<<"'"<<endl;
// Encode
unsigned int writePaddChars = (3-s.length()%3)%3;
string base64(it_base64_t(s.begin()),it_base64_t(s.end()));
base64.append(writePaddChars,'=');
cout << "Base64 representation: " << base64 << endl;
// Decode
unsigned int paddChars = count(base64.begin(), base64.end(), '=');
std::replace(base64.begin(),base64.end(),'=','A'); // replace '=' by base64 encoding of '\0'
string result(it_binary_t(base64.begin()), it_binary_t(base64.end())); // decode
result.erase(result.end()-paddChars,result.end()); // erase padding '\0' characters
cout << "Decoded: " << result << endl;
return 0;
}
ध्यान दें कि मैं insert_linebreaks
और remove_whitespace
iterators कहा, ताकि base64 उत्पादन अच्छी तरह से स्वरूपित किया गया है और लाइन टूट के साथ बेस 64 इनपुट डीकोड किया जा सकता। हालांकि ये वैकल्पिक हैं।
विभिन्न इनपुट तार जो विभिन्न गद्दी की आवश्यकता के साथ रन:
$ ./base64example
Hello World!
Your string is: 'Hello World!'
Base64 representation: SGVsbG8gV29ybGQh
Decoded: Hello World!
$ ./base64example
Hello World!!
Your string is: 'Hello World!!'
Base64 representation: SGVsbG8gV29ybGQhIQ==
Decoded: Hello World!!
$ ./base64example
Hello World!!!
Your string is: 'Hello World!!!'
Base64 representation: SGVsbG8gV29ybGQhISE=
Decoded: Hello World!!!
आप इस online-encoder/decoder साथ बेस 64 तार देख सकते हैं।
बूस्ट सी ++ लाइब्रेरी का उपयोग कर बेस 64 एन्कोडिंग फ़ंक्शंस: http://stackoverflow.com/questions/34680998/attempt-to-decode-a-value-not-in-base64-char-set – ap6491