宽窄字符之间的转换
来源:互联网 发布:动漫绘画软件手机版 编辑:程序博客网 时间:2024/04/27 20:37
1、宽字节转窄字节(注意内存的释放delete):
// 宽字符转窄字符char *WideCharToAnsi(wchar_t *pWideChar){if (pWideChar == NULL){return NULL;}char *pszBuf = NULL;int needBytes = WideCharToMultiByte(CP_ACP, 0, pWideChar, -1, NULL, 0, NULL, NULL);if (needBytes > 0){pszBuf = new char[needBytes+1];ZeroMemory(pszBuf, (needBytes+1)*sizeof(char));WideCharToMultiByte(CP_ACP, 0, pWideChar, -1, pszBuf, needBytes, NULL, NULL);}return pszBuf;}
2、调用示例:
// Test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "Windows.h"#include <string.h>#include <iostream>using namespace std;char *WideCharToAnsi(wchar_t *pWideChar);int _tmain(int argc, _TCHAR* argv[]){wchar_t szStr[] = L"Hello World!";char *pStr = WideCharToAnsi(szStr);wcout << szStr << endl;cout << pStr << endl;delete [] pStr; // 释放内存空间,防止内存泄漏getchar();return 0;}// 宽字符转窄字符char *WideCharToAnsi(wchar_t *pWideChar){if (pWideChar == NULL){return NULL;}char *pszBuf = NULL;int needBytes = WideCharToMultiByte(CP_ACP, 0, pWideChar, -1, NULL, 0, NULL, NULL);if (needBytes > 0){pszBuf = new char[needBytes+1];ZeroMemory(pszBuf, (needBytes+1)*sizeof(char));WideCharToMultiByte(CP_ACP, 0, pWideChar, -1, pszBuf, needBytes, NULL, NULL);}return pszBuf;}
结果:
3、窄字节转宽字节(注意内存的释放delete):
// 窄字节转宽字节wchar_t *AnsiCharToWide(char *pChar){if (pChar == NULL){return NULL;}wchar_t *pszBuf = NULL;int needWChar = MultiByteToWideChar(CP_ACP, 0, pChar, -1, NULL, 0);if (needWChar > 0){pszBuf = new wchar_t[needWChar+1];ZeroMemory(pszBuf, (needWChar+1)*sizeof(wchar_t));MultiByteToWideChar(CP_ACP, 0, pChar, -1, pszBuf, needWChar);}return pszBuf;}
4、调用示例:
// Test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "Windows.h"#include <string.h>#include <iostream>using namespace std;wchar_t *AnsiCharToWide(char *pChar);int _tmain(int argc, _TCHAR* argv[]){char szStr[] = "Hello World!";wchar_t *pStr = AnsiCharToWide(szStr);cout << szStr << endl;wcout << pStr << endl;delete [] pStr; // 释放内存空间,防止内存泄漏getchar();return 0;}// 窄字节转宽字节wchar_t *AnsiCharToWide(char *pChar){if (pChar == NULL){return NULL;}wchar_t *pszBuf = NULL;int needWChar = MultiByteToWideChar(CP_ACP, 0, pChar, -1, NULL, 0);if (needWChar > 0){pszBuf = new wchar_t[needWChar+1];ZeroMemory(pszBuf, (needWChar+1)*sizeof(wchar_t));MultiByteToWideChar(CP_ACP, 0, pChar, -1, pszBuf, needWChar);}return pszBuf;}
结果:
5、以上两个函数实际上是利用MultiByteToWideChar和WideCharToMultiByte来实现的,实际上还可以利用A2W、W2A、T2A、T2W 等宏进行转换。
6、使用示例:
// Test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "Windows.h"#include <atlconv.h>#include <string.h>#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){wchar_t szStr[] = L"Hello World!";USES_CONVERSION;char *pStr = W2A(szStr);wchar_t *pStr2 = A2W(pStr);wcout << szStr << endl;cout << pStr << endl;wcout << pStr2 << endl;getchar();return 0;}
说明:
(1)宏返回的地址空间是从栈上面申请的,不必调用delete释放;
(2)不要在一个函数的循环体中使用 字符转换宏,可能引起栈溢出。
0 0
- 宽窄字符之间的转换
- C++中数字与字符串之间的转换(包括C++11新标准和宽窄字符转换)
- C++中数字与字符串之间的转换(包括C++11新标准和宽窄字符转换)
- 简单转换宽窄字符
- 宽窄字符相互转换
- 宽窄字符转换
- 宽窄字符之间的转换——字符串处理(三)
- 实习随手记-宽窄字符串之间的转换
- wchar、tchar、L等宽窄字符的含义与转换
- w_char*和char *转换宽窄字符
- c++版 宽窄字符 转换方法
- 字符之间的转换
- 字符之间的转换
- 宽窄字符系列
- iconv()字符之间的转换
- 字符编码之间的转换
- 字符类型之间的转换
- 字符数字之间的转换
- 展现最好的自己
- spring多数据源配置
- Python宝典第20章:功能强大的正则表达式
- eclipse字母大小写转换的快捷键
- 指针常量 常量指针详细讲解
- 宽窄字符之间的转换
- DOM PULL SAX 三种方式解析XML
- Unity 动态改变天空盒 材质
- Effective JavaScript Item 34 在prototype上保存方法
- DSP学习总结——CAN通信
- 飘逸的python - descriptor(描述器)就是这么回事
- 为什么虚函数可以为虚函数
- Java调用bat文件
- Office 办公软件基本操作