C ++ how to convert wide string to base64?

What is the best way to convert a wide string to base64?

+3
source share
3 answers

Octet (8-bit characters) -> Base64 (6-bit) conversion is converted to bytes rather than characters, so it works the same regardless of your string encoding.


To be clear: Base64 is not a character encoding. The sender and receiver need to agree on the character encoding (ASCII, UTF-8, UTF-16, UCS-2, etc.), as well as the transport method (Base64, gzip, etc.).

+6
source

To encode some data on base64, you can use the Base64 class from the Xerces library. It might look like this:

std::wstring input_string = SOME; // some wide string
// keep it in contiguous memory (the following string is not needed in C++0x)
std::vector<wchar_t> raw_str( input_string.begin(), input_string.end() );

XMLSize_t len;
XMLByte* data_encoded = xercesc::Base64::encode( reinterpret_cast<const XMLByte*>(&raw_str[0]), raw_str.size()*sizeof(wchar_t), &len );
XMLCh* text_encoded = xercesc::XMLString::transcode( reinterpret_cast<char*>(data_encoded) );

// here text_encoded is encoded text
// do some with text_encoded

XMLString::release( &text_encoded );
XMLString::release( reinterpret_cast<char**>(&data_encoded) );
+1
source

Visual ++ MFC, . Base64Encode Base64Decode.

0

All Articles