关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与多字节字符的转换)以及字符串的转换代码测试
#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 ;
}
关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与多字节字符的转换)以及字符串的转换代码测试的更多相关文章
- 通过编写串口助手工具学习MFC过程——(三)Unicode字符集的宽字符和多字节字符转换
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...
- unicode字符和多字节字符的相互转换接口
作者:朱金灿 来源:http://blog.csdn.net/clever101 发现开源代码的可利用资源真多,从sqlite3的源码中抠出了几个字符转换接口,稍微改造下了发现还挺好用的.下面是实现代 ...
- ANIS与UNICODE字符格式转换:MultiByteToWideChar() 和WideCharToMultiByte() 函数
资料来自: http://blog.csdn.net/holamirai/article/details/47948745 http://www.cnblogs.com/wanghao111/arch ...
- MultiByteToWideChar和WideCharToMultiByte用法详解, ANSI和UNICODE之间的转换
//========================================================================//TITLE:// MultiByteToW ...
- MultiByteToWideChar和WideCharToMultiByte用法详解
今天写ini文件的时候发现的问题: TCHAR temp[]; //strcpy_s(temp, request.newVersion); MultiByteToWideChar(CP_ACP, , ...
- UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)
继上集故事<多字符集(ANSI)和UNICODE及字符串处理方式准则 >,我们现在有一些特殊需求: 有时候我们的字符串是多字符型,我们却需要使用宽字符型:有的时候却恰恰相反. Window ...
- 使用Unicode(宽字节字符集);多字节字符集中定义宽字节变量
2012-03-25 14:54 (分类:计算机程序) 2.2 宽字符和C 宽字符不一定是Unicode.Unicode是宽字符集的一种.然而,因为本书的焦点是Windows而不是C执行的理论,所以书 ...
- C++多字节字符转换为宽字符的两种方法
目前知道有两种方式:可以提供宽字符与ANSI字符之间的转换, 第一种由COM库提供的函数 char* _com_util::ConvertBSTRToString(BSTR ); BSTR _com ...
- warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
------问题-------------------- Qt项目使用 VC++ 编译器出现此错误. warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符.请将该文件保存为 ...
随机推荐
- 寻找最大的k个数问题
这是编程之美书第2.5节的一道题目. 各种解法: 解法一,用nlgn复杂度的排序算法对数组进行从大到小排序,取前K个.但这方法做了两件不必要做的事:它对想得到的K个数进行了排序,对不想得到的n-K个数 ...
- 【Unity/SVN】使用SVN管理Unity项目
本文转载自:http://blog.csdn.net/neil3d/article/details/38437237 Unity提供了自己的XXXServer,不过大家评论好像不是很好用,主要是不支持 ...
- Windows 2008 Server搭建Radius服务器的方法
原地址:http://service.tp-link.com.cn/detail_article_1113.html (图拷贝不过来) Windows 2008 Server搭建Radius服务器的方 ...
- 标准Drupal7安装中文翻译出错解决办法
这个问题在网上解决的方案一致都是说在\sites\default\settings.php文件,在最后增加以下两行: ini_set('memory_limit', '1280M'); //加大php ...
- SGU 120 Archipelago (简单几何)
120. Archipelago time limit per test: 0.25 sec. memory limit per test: 4096 KB Archipelago Ber-Isla ...
- dubbo 常见错误 通配符的匹配很全面, 但无法找到元素 'dubbo:application' java.lang.reflect.MalformedParameterizedTypeException 通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明。 Unsupported major.minor version 52.0 (unable to l
dubbo 常见错误 1. Caused by: java.lang.reflect.MalformedParameterizedTypeException 启动时报错,原因是dubbo 依赖 spr ...
- Windows通用知识讲解一
Window应用程序的类型 --控制台程序Console DOS程序,本身没有窗口,通过Windows DOS窗口执行 --窗口程序 拥有自己的窗口,可以与用户交互 --库程序 存放代码.数据的程序, ...
- 最小顶点覆盖(Minimum Vertex Cover)与最大独立集(Maximum Independent Set)
问题描述:就是在图中找最小的点集,使得覆盖所有边. 和独立集等价:独立集问题:在图中找最大的点集,使得点集内的所有点互不相连. 引理:顶点覆盖集和独立集互补. 上面这个引理使得这两个问题可以相互规约, ...
- 关于矢量图片资源向后兼容:CompatVectorFromResourcesEnabled标志的使用
StackOverflow上摘抄的: some things have changed since I asked this question so I will answer it myself.W ...
- C++函数的高级特性
对比于 C 语言的函数,C++增加了重载(overloaded).内联(inline).const 和 virtual 四种新机制.其中重载和内联机制既可用于全局函数也可用于类的成员函数,const ...