[英]C++ how convert wide string to base64?
将宽字符串转换为base64的最佳方法是什么?
八位字节(8位符号) - > Base64(6位符号)转换适用于字节,而不是字符,因此它的工作方式与字符串编码无关。
需要说明的是:Base64不是字符编码。 发送者和接收者需要就字符编码(ASCII,UTF-8,UTF-16,UCS-2等)以及传输方法(Base64,gzip等)达成一致。
要将某些数据编码为base64,您可以使用Xerces库中的Base64类。 它可能如下所示:
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's text_encoded is encoded text
// do some with text_encoded
XMLString::release( &text_encoded );
XMLString::release( reinterpret_cast<char**>(&data_encoded) );
如果您使用Visual C ++与MFC,已经有一个库来执行此操作。 查看Base64Encode
和Base64Decode
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.