c++多字节与宽字节字符串转换(windows平台)
来源:互联网 发布:mac升级bootcamp 6.0 编辑:程序博客网 时间:2024/05/22 15:35
转自http://www.asarea.cn/showdiary.jsp?id=193
许久没有写技术日志了,今天写一篇关于字符串转换的。
大家都知道,在标准c++中有string和wstring,前者为多字节ANSI字串,后者为宽字节wide字串(UTF-16)。
在一些应用中常常需要在二者之间以及与UTF-8之间进行字串转换,这里提供一个自己写的类,用于之间的转换。
原理是利用windows的api:WideCharToMultiByte、MultiByteToWideChar。
这两个api的第一个参数就是代码页,其中CP_ACP代表ANSI CODE PAGE,而CP_UTF8自然代表UTF-8。
注:由于用到了windows api,所以只适用windows平台。
/**
* Author: ATHER Shu 2009.3.20
* StringUtil类:用于ANSI、UTF-16、UTF-8之间的字串转换
* 功能:
* 1.UTF-16转ANSI ws2s
* 2.ANSI转UTF-16 s2ws
* 3.UTF-16转UTF-8 ws2utf8
* 4.UTF-8转UTF-16 utf82ws
* http://www.asarea.cn
* ATHER Shu(AS)
*/
#include <string>
#include <Windows.h>
using namespace std;
class StringUtil
{
public:
static string ws2s(const wstring& ws)
{
const wchar_t* _Source = ws.c_str();
size_t _Dsize = WideCharToMultiByte(CP_ACP, 0, _Source, -1, NULL, 0, NULL, NULL);
char *_Dest = new char[_Dsize];
WideCharToMultiByte(CP_ACP, 0, _Source, -1, _Dest, _Dsize, NULL, NULL);
string result = _Dest;
delete []_Dest;
return result;
}
static wstring s2ws(const string& s)
{
const char* _Source = s.c_str();
size_t _Dsize = MultiByteToWideChar(CP_ACP, 0, _Source, -1, NULL, 0);
wchar_t *_Dest = new wchar_t[_Dsize];
MultiByteToWideChar(CP_ACP, 0, _Source, -1, _Dest, _Dsize);
wstring result = _Dest;
delete []_Dest;
return result;
}
static string ws2utf8(const wstring& ws)
{
const wchar_t* _Source = ws.c_str();
size_t _Dsize = WideCharToMultiByte(CP_UTF8, 0, _Source, -1, NULL, 0, NULL, NULL);
char *_Dest = new char[_Dsize];
WideCharToMultiByte(CP_UTF8, 0, _Source, -1, _Dest, _Dsize, NULL, NULL);
string result = _Dest;
delete []_Dest;
return result;
}
static wstring utf82ws(const string& s)
{
const char* _Source = s.c_str();
size_t _Dsize = MultiByteToWideChar(CP_UTF8, 0, _Source, -1, NULL, 0);
wchar_t *_Dest = new wchar_t[_Dsize];
MultiByteToWideChar(CP_UTF8, 0, _Source, -1, _Dest, _Dsize);
wstring result = _Dest;
delete []_Dest;
return result;
}
};
* Author: ATHER Shu 2009.3.20
* StringUtil类:用于ANSI、UTF-16、UTF-8之间的字串转换
* 功能:
* 1.UTF-16转ANSI ws2s
* 2.ANSI转UTF-16 s2ws
* 3.UTF-16转UTF-8 ws2utf8
* 4.UTF-8转UTF-16 utf82ws
* http://www.asarea.cn
* ATHER Shu(AS)
*/
#include <string>
#include <Windows.h>
using namespace std;
class StringUtil
{
public:
static string ws2s(const wstring& ws)
{
const wchar_t* _Source = ws.c_str();
size_t _Dsize = WideCharToMultiByte(CP_ACP, 0, _Source, -1, NULL, 0, NULL, NULL);
char *_Dest = new char[_Dsize];
WideCharToMultiByte(CP_ACP, 0, _Source, -1, _Dest, _Dsize, NULL, NULL);
string result = _Dest;
delete []_Dest;
return result;
}
static wstring s2ws(const string& s)
{
const char* _Source = s.c_str();
size_t _Dsize = MultiByteToWideChar(CP_ACP, 0, _Source, -1, NULL, 0);
wchar_t *_Dest = new wchar_t[_Dsize];
MultiByteToWideChar(CP_ACP, 0, _Source, -1, _Dest, _Dsize);
wstring result = _Dest;
delete []_Dest;
return result;
}
static string ws2utf8(const wstring& ws)
{
const wchar_t* _Source = ws.c_str();
size_t _Dsize = WideCharToMultiByte(CP_UTF8, 0, _Source, -1, NULL, 0, NULL, NULL);
char *_Dest = new char[_Dsize];
WideCharToMultiByte(CP_UTF8, 0, _Source, -1, _Dest, _Dsize, NULL, NULL);
string result = _Dest;
delete []_Dest;
return result;
}
static wstring utf82ws(const string& s)
{
const char* _Source = s.c_str();
size_t _Dsize = MultiByteToWideChar(CP_UTF8, 0, _Source, -1, NULL, 0);
wchar_t *_Dest = new wchar_t[_Dsize];
MultiByteToWideChar(CP_UTF8, 0, _Source, -1, _Dest, _Dsize);
wstring result = _Dest;
delete []_Dest;
return result;
}
};
- c++多字节与宽字节字符串转换(windows平台)
- c++多字节与宽字节字符串转换(windows平台)
- c++多字节与宽字节字符串转换(windows平台)
- 字符串:多字节与宽字节转换
- 宽字节字符串多字节字符串转换
- 多字节与宽字节字符串的相互转换
- 多字节与宽字节字符串 的相互转换
- Windows 的多字节与宽字节的转换
- C++多字节与宽字节 转换
- 宽字节与多字节转换函数
- 宽字节与多字节转换
- VC多字节字符串和宽字节字符串转换
- c总结11 -- 多字节与宽字节间的转换(wchar_t与char转换)
- 多字节字符串转宽字节字符串(windows)
- 多字节字符串与宽字节字符串的相互转化
- 多字节与宽字符串转换
- 多字节与宽字节相互转换的方法
- 多字节与宽字节相互转换的方法
- JAVA性能优化及垃圾回收机制
- magento -- 搜索功能的后台管理技巧
- .net
- Context的作用
- 关于ORACLE分区表的概念及操作
- c++多字节与宽字节字符串转换(windows平台)
- EFI Shell 命令参考
- Netscreen 简单的上网和端口映射设置
- oop学习例子2
- VC中字符串取子串总结(转)
- javascript的history.go(-1)
- JQuery筛选器对.和[]的识别
- EFI Shell 命令
- C++中四种类型转换运算符的使用方法