Unicode version of base64 encoding/ decoding

Posted by Yan Cheng CHEOK on Stack Overflow See other posts from Stack Overflow or by Yan Cheng CHEOK
Published on 2011-01-11T03:49:15Z Indexed on 2011/01/11 5:53 UTC
Read the original article Hit count: 190

Filed under:

I am using base64 encoding/decoding from http://www.adp-gmbh.ch/cpp/common/base64.html

It works pretty well with the following code.

  const std::string s = "I Am A Big Fat Cat" ;

  std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(s.c_str()), s.length());
  std::string decoded = base64_decode(encoded);

  std::cout << _T("encoded: ") << encoded << std::endl;
  std::cout << _T("decoded: ") << decoded << std::endl;

However, when comes to unicode

namespace std
{
#ifdef _UNICODE
    typedef wstring tstring;
#else
    typedef string tstring;
#endif
}
const std::tstring s = _T("I Am A Big Fat Cat");

How can I still make use of the above function?

Merely changing

std::string base64_encode(unsigned TCHAR const* , unsigned int len);
std::tstring base64_decode(std::string const& s);

will not work correctly.

(I expect base64_encode to return ASCII. Hence, std::string should be used instead of std::tstring)

© Stack Overflow or respective owner

Related posts about c++