宽字符和窄字符的转换接口
来源:互联网 发布:淘宝玉壶冰烟油怎么样 编辑:程序博客网 时间:2024/05/16 15:37
作者:朱金灿
来源:http://blog.csdn.net/clever101/article/details/7874595
宽字符和窄字符的转换需求很经常会遇到,今天从网上找了两个函数,修改了一下,奉献给大家。
#include <string> #include <assert.h> std::wstring toWideString( const char* pStr,int len) { assert(pStr) ; assert(len >= 0 || len == -1, _T("Invalid string length: ") << len ) ; // figure out how many wide characters we are going to get int nChars = MultiByteToWideChar( CP_ACP , 0 , pStr , len , NULL , 0 ) ; if ( len == -1 ) -- nChars ; if ( nChars == 0 ) return L"" ; // convert the narrow string to a wide string // nb: slightly naughty to write directly into the string like this std::wstring buf; buf.resize(nChars); ::MultiByteToWideChar(CP_ACP,0,pStr,len,const_cast<wchar_t*>(buf.c_str()),nChars); return buf ; } std::wstring toWideString(const std::string& strA) { const char* pStr = strA.c_str(); int len = strA.length(); return toWideString(pStr,len); } std::string toNarrowString( const wchar_t* pStr,int len) { // figure out how many narrow characters we are going to get assert(pStr) ; assert(len >= 0 || len == -1 , _T("Invalid string length: ") << len ) ; int nChars = WideCharToMultiByte( CP_ACP , 0 , pStr , len , NULL , 0 , NULL , NULL ) ; if ( len == -1 ) -- nChars ; if ( nChars == 0 ) return "" ; // convert the wide string to a narrow string // nb: slightly naughty to write directly into the string like this std::string buf ; buf.resize(nChars); WideCharToMultiByte(CP_ACP,0,pStr,len,const_cast<char*>(buf.c_str()),nChars,NULL,NULL); return buf ; } std::string toNarrowString(const std::wstring& strW) { const wchar_t* pStr = strW.c_str(); int len = strW.length(); return toNarrowString(pStr,len); }
参考文献:
1. 如何升级基于STL的应用来支持Unicode
- 宽字符和窄字符的转换接口
- 宽字符和窄字符的转换接口
- 宽字符和窄字符的转换接口
- unicode字符和多字节字符的相互转换接口
- VS中常见的宽字符和窄字符转换
- 宽字符和窄字符之间的转换
- C++中宽字符和窄字符的相互转换
- 宽字符 和 字符 转换
- 宽字符与窄字符的转换
- 宽字符wchar_t* 和字符char*的介绍和转换
- ANSI字符和UNICODE字符的转换
- 字符和字符值的转换
- 字符和字符值之间的转换
- 宽字符和多字节(窄字符)转换
- C++ 宽字符和短字符转换
- 宽字符和UTF8之间的转换
- 字符 和 数值的转换
- 普通字符与宽字符的转换
- 一个像素占多大内存 多少字节
- 创建一个链表
- about hashtags of tweet
- 反向输出一个链表
- 使用Aspectj进行AOP开发
- 宽字符和窄字符的转换接口
- 连接两个链表
- tcpdump简介
- 八进制转换为十进制
- 全排列之探索最简洁写法
- JQuery---右键菜单版本一
- 有关Java包装类及其拆箱装箱的小结
- 【C/C++】编程常用命名方法
- BUG:8993720