utf8, unicode, gb2312 编码转换vc++

来源:互联网 发布:学校排课软件 编辑:程序博客网 时间:2024/05/16 10:02

 
 void unicodeToUTF8(const wstring &src, string& result)
 {
  int n = WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0 );

  result.resize(n);

  ::WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0 );
 }

 

 void utf8ToUnicode(const string& src, wstring& result)
 {
  int n = MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, NULL, 0 );
  
  result.resize(n);
  
  ::MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, (TCHAR*)result.c_str(), result.length());
 }

 

 inline void unicodeToGB2312(string& result, const wstring& wstr)
 {
  int n = WideCharToMultiByte( CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0 );
  
  result.resize(n);
  
  ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, (char*)result.c_str(), n, 0, 0 );
 }

原创粉丝点击