#pragma once
#include <stdio.h> //getchar()
#include <tchar.h>
#include <stdlib.h> //sysytem()
#include <string> //std
#include <atlstr.h> //cstring
#include <iostream> //cout using namespace std;
using std::wcout; int _tmain(int argc, _TCHAR* argv[])
{
/***** char* 转换 cstring *********/
//方式一 直接赋值
//char chArray[] = "This a cat!";
// char* p = "This a cat!";
//LPSTR p = "This a cat!";
//CString cstr = p;
//方式二 format格式转化
//CString cstr1;
//cstr1.Format("%s",p);
//cout<< cstr1 << endl;
/************ cstring转换char* ************/
//方式一(LPSTR)(LPCTSTR)强转
//CString thecstring("this a cat!");
//char *cp;
//*cp = (LPSTR)(LPCTSTR)thecstring;
//方式二 使用strcpy
//cp = new TCHAR[thecstring.GetLength() + 1];
//_tcscpy_s(cp,thecstring.GetLength() + 1, thecstring);
//方式三 使用CString::GetBuffer()
//CString s(_T("this a cat!"));
//LPTSTR cp = s.GetBuffer();
//cout<< cp << endl;
//if (NULL != cp)
//{
// *cp = _T('\0');
//*cp = _T('123');输出为 3his a cat 截断了字符
//}
//s.ReleaseBuffer(); //cout<< cp << endl; /********* WideCharToMultiByte(Unicode to Ansi) *************/
wchar_t wText[] = {L"宽字符转成窄字符!"};
DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -, NULL,,NULL,FALSE);
char* psText;
psText = new char[dwNum];
if (!psText)
{
delete []psText;
}
WideCharToMultiByte(CP_OEMCP, NULL, wText, -,psText, dwNum, NULL, FALSE); cout << psText << endl;
delete []psText;
psText = NULL; /********* MultiByteToWideChar(Ansi to Unicode) *************/
char cText[] = {"窄字符转成宽字符!"};
DWORD dwNum1 = MultiByteToWideChar(CP_ACP, NULL,cText, -, NULL, );
wchar_t* pwText;
pwText = new wchar_t[dwNum1];
if (!pwText)
{
delete []pwText;
}
MultiByteToWideChar(CP_ACP, NULL, cText, -, pwText, dwNum1);
wchar_t wsz[] = L"ni hao a!";//宽字符和宽字符串常量前要加L
//变量里存放的是中文的话,要设置区域使用wcout.imbue(locale("chs"));
//才能输出变量里面的中文,不然输出的是变量地址
    //还可以设置全局函数setlocale(LC_ALL,"Chinese-simplified");
     wcout.imbue(locale("chs"));
std::wcout << wsz << std::endl;
std::wcout << pwText << std::endl;
delete []pwText;
pwText = NULL;
//getchar();
system("pause");
return ;
}
WideCharToMultiByte
函数功能:该函数映射一个unicode字符串到一个多字节字符串。
函数原型:
int WideCharToMultiByte(
UINT CodePage, //指定执行转换的代码页
DWORD dwFlags, //允许你进行额外的控制,它会影响使用了读音符号(比如重音)的字符
LPCWSTR lpWideCharStr, //指定要转换为宽字节字符串的缓冲区
int cchWideChar, //指定由参数lpWideCharStr指向的缓冲区字符个数
LPSTR lpMultiByteStr, //指向接收被转换字符串的缓冲区
int cchMultiByte, //指定由参数lpMultiByteStr指向的缓冲区最大值
LPCSTR lpDefaultChar, //遇到一个不能转换的宽字符,函数便会使用pDefaultChar参数指向的字符
LPBOOL pfUsedDefaultChar //至少有一个字符不能转换为其多字节形式,函数就会把这个变量设为TRUE
);
参数:
CodePage:指定执行转换的代码页,这个参数可以为系统已安装或有效的任何代码页所给定的值。你也可以指定其为下面的任意一值:
CP_ACP:ANSI代码页;CP_MACCP:Macintosh代码页;CP_OEMCP:OEM代码页;
CP_SYMBOL:符号代码页(42);CP_THREAD_ACP:当前线程ANSI代码页;
CP_UTF7:使用UTF-7转换;CP_UTF8:使用UTF-8转换。
函数功能:该函数映射一个字符串到一个宽字符(unicode)的字符串。由该函数映射的字符串没必要是多字节字符组。
函数原型:
int MultiByteToWideChar(
UINT CodePage,
DWORD dwFlags,
LPCSTR lpMultiByteStr,
int cchMultiByte,
LPWSTR lpWideCharStr,
int cchWideChar
);

最新文章

  1. Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
  2. wex5 实战 单页模式下的多页面数据同步
  3. 使用ABP时报错“UPDATE 语句与 FOREIGN KEY SAME TABLE 约束&quot;FK_dbo.AbpUsers_dbo.AbpUsers_LastModifierUserId&quot;冲突”的解决办法
  4. Managed DirectX中的DirectShow应用(简单Demo及源码)
  5. 配置ogg异构oracle-mysql 双向同步注意事项
  6. android基础(三)ContentProvider
  7. iOS 拨打电话三种方法
  8. EPLAN部件库之共享方法
  9. [moka同学笔记]一、Yii2.0课程笔记(魏曦老师教程)
  10. 查看mysqll账号信息
  11. spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描
  12. android 访问SMS短信收件箱
  13. OPTIMIZE TABLE
  14. asp.net mvc 文件压缩下载
  15. Andorid自动读取短信验证码
  16. [DeeplearningAI笔记]Multi-class classification多类别分类Softmax regression_02_3.8-3.9
  17. HBase 运维分析
  18. mysql 1194 – Table ‘tbl_video_info’ is marked as crashed and should be repaired 解决方法
  19. app个推(透传消息)
  20. 20.Module 的语法

热门文章

  1. c#打印(转)
  2. wp中的双向绑定
  3. excel自定义函数添加和使用方法
  4. Log4j 2使用教程&lt;转&gt;
  5. Spring Cache 源码解析
  6. nfs服务器与客户端配置
  7. MVC教程二:从控制器中获取URL的值
  8. Qt Q_DECLARE_METATYPE说明——让自定义的类型设置到QVariant
  9. 【转】【Linux】Linux 命令行快捷键
  10. 【转】MFC 数据绑定 DoDataExchange( )