多字节与宽字符相互转换
2012-09-11 15:38
423 查看
多字节字符串与宽字符串的转换可使用C API者Win32 API.
C API: mbstowcs,wcstombs
Win32 API: MultiByteToWideChar, WideCharToMultiByte
下面着重介绍Win32 API的用法,C API的用法较为简单可参照Win32 API。
首先是WideCharToMultiByte
通常你需要配置4个参数(其他参数如是使用即可),红色标记的部分。
依次是源宽字符串,需要转换的长度(-1,则为转换整个字符串),目标多字节字符串,目标缓冲区长度。
返回值表示转换为目标多字节字符串实际需要的长度(包括结束符)。
所以通常需要调用WideCharToMultiByte两次:第一次产生目标缓冲区长度,第二次产生目标字符串,像下面这样
wchar_t* wcs = L"中国,你好!I Love You!";
int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, wcs, -1, NULL, 0, NULL, NULL);
char* mbs = new char[ lengthOfMbs ];
WideCharToMultiByte( CP_ACP, 0, wcs, -1, mbs, lengthOfMbs, NULL, NULL);
delete mbs;
mbs = NULL;
MultiByteToWideChar的用法类似
char* mbs = "中国,你好!I Love You!";
int lengthOfWcs = MultiByteToWideChar( CP_ACP, 0, mbs, -1, NULL, 0 );
wchar_t* wcs = new wchar_t[ lengthOfWcs ];
MultiByteToWideChar( CP_ACP, 0, mbs, -1, wcs, lengthOfWcs );
delete wcs;
wcs = NULL;
下面两个函数封装了转换过程
#include
#include
std::string WcsToMbs( const std::wstring& wcs ) {
int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, wcs.c_str(), -1, NULL, 0, NULL, NULL);
char* mbs = new char[ lengthOfMbs ];
WideCharToMultiByte( CP_ACP, 0, wcs.c_str(), -1, mbs, lengthOfMbs, NULL, NULL);
std::string result = mbs;
delete mbs;
mbs = NULL;
return result;
}
std::wstring MbsToWcs( const std::string& mbs ) {
int lengthOfWcs = MultiByteToWideChar( CP_ACP, 0, mbs.c_str(), -1, NULL, 0 );
wchar_t* wcs = new wchar_t[ lengthOfWcs ];
MultiByteToWideChar( CP_ACP, 0, mbs.c_str(), -1, wcs, lengthOfWcs );
std::wstring result = wcs;
delete wcs;
wcs = NULL;
return result;
}
C API: mbstowcs,wcstombs
Win32 API: MultiByteToWideChar, WideCharToMultiByte
下面着重介绍Win32 API的用法,C API的用法较为简单可参照Win32 API。
首先是WideCharToMultiByte
通常你需要配置4个参数(其他参数如是使用即可),红色标记的部分。
依次是源宽字符串,需要转换的长度(-1,则为转换整个字符串),目标多字节字符串,目标缓冲区长度。
返回值表示转换为目标多字节字符串实际需要的长度(包括结束符)。
所以通常需要调用WideCharToMultiByte两次:第一次产生目标缓冲区长度,第二次产生目标字符串,像下面这样
wchar_t* wcs = L"中国,你好!I Love You!";
int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, wcs, -1, NULL, 0, NULL, NULL);
char* mbs = new char[ lengthOfMbs ];
WideCharToMultiByte( CP_ACP, 0, wcs, -1, mbs, lengthOfMbs, NULL, NULL);
delete mbs;
mbs = NULL;
MultiByteToWideChar的用法类似
char* mbs = "中国,你好!I Love You!";
int lengthOfWcs = MultiByteToWideChar( CP_ACP, 0, mbs, -1, NULL, 0 );
wchar_t* wcs = new wchar_t[ lengthOfWcs ];
MultiByteToWideChar( CP_ACP, 0, mbs, -1, wcs, lengthOfWcs );
delete wcs;
wcs = NULL;
下面两个函数封装了转换过程
#include
#include
std::string WcsToMbs( const std::wstring& wcs ) {
int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, wcs.c_str(), -1, NULL, 0, NULL, NULL);
char* mbs = new char[ lengthOfMbs ];
WideCharToMultiByte( CP_ACP, 0, wcs.c_str(), -1, mbs, lengthOfMbs, NULL, NULL);
std::string result = mbs;
delete mbs;
mbs = NULL;
return result;
}
std::wstring MbsToWcs( const std::string& mbs ) {
int lengthOfWcs = MultiByteToWideChar( CP_ACP, 0, mbs.c_str(), -1, NULL, 0 );
wchar_t* wcs = new wchar_t[ lengthOfWcs ];
MultiByteToWideChar( CP_ACP, 0, mbs.c_str(), -1, wcs, lengthOfWcs );
std::wstring result = wcs;
delete wcs;
wcs = NULL;
return result;
}
相关文章推荐
- VC++ 多字节宽字符相互转换
- ASCII、十六进制、字节数组、字符数组相互转换
- C#字符、字节相互转换
- c#中字符和字节相互转换
- C++多字节字符与宽字节字符相互转换
- unicode字符和多字节字符的相互转换接口
- unicode字符和多字节字符的相互转换接口
- linux下宽字节和多字符之间的转换
- native2ascii 中文字符与Unicode编码相互转换
- 字符数组、String类、StringBuffer三者的相互转换
- 字符数组相互转换成字符串
- C#字节数组与字符串相互转换代码案例
- 字节数组、16进制数的字符串的相互转换
- Android字节、十进制、十六进制、字符串之间的相互转换
- 多字节和宽字符之间的转换方法
- Swift字符与ASCII数值相互转换
- JavaScript十六进制字符串和字节数组相互转换
- 主题:Java整数-字节,字节-字符,字节数组-十六进制字符串转换
- 多字节与宽字节处理及ASCII码页转换及利用宽字符输出所有的字符