几个字符串转换函数(ANSI-->Unicode)
来源:互联网 发布:如何绕过mac地址过滤 编辑:程序博客网 时间:2024/04/29 21:59
都是WINAPI : MultiByteToWideChar的各种应用!
纯C++非MFC版:
/// <summary>/// 字符串转换函数(ANSI-->Unicode)/// </summary>/// <param name="lpMultiByteStr"> 被转换的ANSI字符串 </param> /// <param name="lpWideCharStr"> 接收Unicode字符串的地址,内部初始化 </param> /// <returns>/// The function returns 0 if it does not succeed./// Returns the number of characters written to the buffer indicated by lpWideCharStr if successful/// </returns>/// <remarks>/// </remarks>static int MByteToWChar(const char* lpMultiByteStr, std::unique_ptr<wchar_t>& lpWideCharStr){// Get the required size of the buffer that receives the Unicode string.// If the function succeeds(!=0) and cchWideChar is 0, the return value // is the required size, in characters, for the buffer indicated by lpWideCharStr. DWORD dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpMultiByteStr, -1, NULL, 0);if ( dwMinSize == 0 ){return 0;}lpWideCharStr.reset( new wchar_t[dwMinSize] );// Convert headers from ASCII to Unicode.return MultiByteToWideChar (CP_ACP, 0, lpMultiByteStr, -1, lpWideCharStr.get(), dwMinSize);}
MFC库源代码版:
int __stdcall CMFCControlContainer::UTF8ToString(LPCSTR lpSrc, CString& strDst, int nLength){LPTSTR lpDst = NULL;int count = ::MultiByteToWideChar(CP_UTF8, 0, lpSrc, nLength, NULL, 0);if (count <= 0){return 0;}LPWSTR lpWide = new WCHAR[count + 1];memset(lpWide, 0, (count + 1) * sizeof(WCHAR));::MultiByteToWideChar(CP_UTF8, 0, lpSrc, nLength, lpWide, count);#ifdef _UNICODElpDst = lpWide;#elsecount = ::WideCharToMultiByte(::GetACP(), 0, lpWide, -1, NULL, 0, NULL, 0);if (count > 0){lpDst = new char[count + 1];memset(lpDst, 0, count + 1);::WideCharToMultiByte(::GetACP(), 0, lpWide, -1, lpDst, count, NULL, 0);}delete [] lpWide;#endifstrDst = lpDst;delete[] lpDst;return count;}
MFC版本:
char* p = "abc";CString strW(p);
0 0
- 几个字符串转换函数(ANSI-->Unicode)
- Unicode与ANSI字符串转换
- ANSI UNICODE 字符串相互转换
- Unicode 与 ANSI 字符串转换
- Unicode 与 ANSI 字符串转换
- Unicode 与 ANSI 字符串转换
- ansi与unicode字符串转换
- C++字符串转换(Unicode和ANSI互转)
- UNICODE和ANSI字符串的转换
- UNICODE字符串和ANSI字符的转换
- 字符串处理 - ANSI - Unicode - UTF8 转换
- 字符串处理 - ANSI - Unicode - UTF8 转换
- win32 字符串 utf8 unicode ansi相互转换
- 字符串处理 - ANSI - Unicode - UTF8 转换
- 字符串处理 - ANSI - Unicode - UTF8 转换
- 字符串处理 - ANSI - Unicode - UTF8 转换
- unicode和ANSI字符串之间的转换
- 字符串处理 - ANSI - Unicode - UTF8 转换
- count缓存设计问题总结
- POJ 2418-Hardwood Species(map)
- 基于语音识别的微博签到系统
- Predefined Access Levels
- 源代码防泄密软件分析
- 几个字符串转换函数(ANSI-->Unicode)
- Easyui 结合Pluplaod插件的上传
- 浪潮ERP技术支持知识库
- Android菜单详解(一)——理解android中的Menu
- plupload插件结合struts上传文件
- 使用vs2008制作dll文件,生成动态链接库
- three-tier architectur 三层模型
- Base64编码及其作用
- maven——Myeclipse下通过maven管理项目