c++多字节与宽字节字符串转换(windows平台)
sshong 发表于2009年3月31日 09:03:00 更新于2009年3月31日 09:05:00
许久没有写技术日志了,今天写一篇关于字符串转换的。
大家都知道,在标准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平台。
大家都知道,在标准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.me
* 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;
}
};
评论
暂无评论添加评论
分类
琐碎文字 As3&Flex RIA UG English CodingArt C++ PHP Webserver E音乐盒 Unity3d C# JS&Html5 Tools mobile golang AI 最近发表
- claude code / codex的一些配置(2026年5月5日 17:38:10)
- 2026年5月5日(2026年5月5日 17:27:39)
- js的锁以及异步调用相关(2024年11月30日 10:58:51)
- golang学习之函数/方法/接口(2022年1月6日 17:50:24)
- golang学习之零值(2022年1月6日 16:38:10)
- hello, 2018(2018年1月15日 22:47:25)
- 字体类型名词解释(2015年1月18日 11:29:14)
- 获取mysql表注释以及列注释(2014年11月13日 15:56:32)
- php连接ms sql数据库的一些问题(2014年9月15日 20:32:14)
- virtualbox虚拟网络:NAT&bridge桥接网络(2014年8月25日 22:51:35)
最近回复