Ucs和Utf8的互转

来源:互联网 发布:银行家算法过程详解 编辑:程序博客网 时间:2024/06/03 21:42

1:Ucs转成Utf8

BOOL CCommenDlg::UcsToUtf8(const CStringW &strUcs, CStringA &strUtf8){// Ucs 转换为 Utf8  int iRet = ::WideCharToMultiByte(CP_UTF8, 0, strUcs, -1, NULL, 0, NULL, NULL);  if (iRet == 0)  return FALSE;  CHAR *szBuf = new CHAR[iRet];  iRet = ::WideCharToMultiByte(CP_UTF8, 0, strUcs, -1, szBuf, iRet, NULL, NULL);  if (iRet == 0)  return FALSE;  strUtf8 = szBuf;  delete [] szBuf;  return TRUE;  }

2:Utf8转成Ucs

BOOL CCommenDlg::Uft8ToUcs(const CStringA &strUtf8, CStringW &strUcs){// Utf8 转换为 Unicode  int iRet = ::MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL, 0);  if (iRet == 0)  return FALSE;  WCHAR *szBuf = new WCHAR[iRet];  iRet = ::MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, szBuf, iRet);  if (iRet == 0)  return FALSE;  strUcs = szBuf;  delete [] szBuf;  return TRUE;  }


原创粉丝点击