VS中常见的宽字符和窄字符转换
2016-08-25 09:02
232 查看
一、char* 转 wchar*
wchar_t* C2W(const char* str)
{
int len = MultiByteToWideChar(CP_OEMCP, 0, str, -1/*null terminated*/, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
MultiByteToWideChar(CP_OEMCP, 0, str, -1/*null terminated*/, wstr, len);
return wstr;
}
二、wchar* 转 char*
char* W2C(const wchar_t *pwstr)
{
int nlength = wcslen(pwstr);
//获取转换后的长度
int nbytes = WideCharToMultiByte(0, 0, pwstr, nlength, NULL, 0, NULL, NULL);
char* pcstr = new char[nbytes + 1];
// 通过以上得到的结果,转换unicode 字符为ascii 字符
WideCharToMultiByte(0, 0, pwstr, nlength, pcstr, nbytes, NULL, NULL);
pcstr[nbytes] = '\0';
return pcstr;
}
wchar_t* C2W(const char* str)
{
int len = MultiByteToWideChar(CP_OEMCP, 0, str, -1/*null terminated*/, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
MultiByteToWideChar(CP_OEMCP, 0, str, -1/*null terminated*/, wstr, len);
return wstr;
}
二、wchar* 转 char*
char* W2C(const wchar_t *pwstr)
{
int nlength = wcslen(pwstr);
//获取转换后的长度
int nbytes = WideCharToMultiByte(0, 0, pwstr, nlength, NULL, 0, NULL, NULL);
char* pcstr = new char[nbytes + 1];
// 通过以上得到的结果,转换unicode 字符为ascii 字符
WideCharToMultiByte(0, 0, pwstr, nlength, pcstr, nbytes, NULL, NULL);
pcstr[nbytes] = '\0';
return pcstr;
}
相关文章推荐
- 各种常见字符类型转换
- python的字符转换常见bug
- 常见几种字符之间的转换
- 常见C++字符转换
- VS开发环境常见编译错误——LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- VS2008多字符和Uincode模式下的通用字符转换
- C++常见字符转换
- 用JSP转换中文字符在WAP里显示
- UTF-8与Unicode字符的相互转换 与 16进制Unicode转换汉字 重用代码
- java中字符转换
- 16进制字符转换为10进制 delphi
- Unicode字符到GB2312的转换
- 【转载】vc常见类型转换
- js中将日期类型转换成字符串型格式
- java中字符转换
- 在VS.NET IDE中集成VSS的常见问题
- 在JAVA里用函数得到日期,并转换成字符
- ascii字符的转换问题(VB中)
- VS.NET调试.NET出现的常见错误
- VS.NET调试 ASP.NET出现的常见错误总结