不要使用wstring
不要使用wstring,尽量使用string。
因为,转换字符串会带来不必要的麻烦。
不过,对于已经设定了字符集(不能改的情况),只能翻阅各种wstring的说明手册和帖子了~
工程属性(Properties)中,默认都是使用Unicode字符集:
Character Set: Use Unicode Character Set
Use Multi-Byte Character Set,多字节字符集,据说已经建议不要使用了。
同样,对于已经设定了字符集(不能改的情况),只能继续翻阅各种wstring的说明手册和帖子,千万不要试图修改字符集的设定~
命令行输出中文,其实很简单:
#include <iostream>int main()
{const char* pChar1 = "中文abc";std::string string1 = pChar1;std::cout << "std::cout, pChar1: " << pChar1 << std::endl;printf("printf %%s, pChar1: %s\n", pChar1);printf("\n");std::cout << "std::cout, string1: " << string1.c_str() << std::endl;printf("printf %%s, string1: %s\n", string1.c_str());/* Output:
std::cout, pChar1: 中文abc
printf %s, pChar1: 中文abcstd::cout, string1: 中文abc
printf %s, string1: 中文abc*/
}
一旦使用了wstring,输出中文就需要使用setlocale:
#include <iostream>
#include <tchar.h>int main()
{const TCHAR* pTCHAR1 = _T("中文abc");// convert TCHAR* to std::wstring
#ifdef UNICODEtypedef std::wstring TSTRING;
#elsetypedef std::string TSTRING;
#endifTSTRING tstring1 = pTCHAR1;std::wstring wstring1(tstring1.begin(), tstring1.end());setlocale(LC_ALL, "zh-CN");//std::locale loc("chs");//std::locale loc("zh-CN");//std::wcout.imbue(loc);std::wcout << "std::wcout, pTCHAR1: " << pTCHAR1 << std::endl;printf("printf %%ls, pTCHAR1: %ls\n", pTCHAR1);printf("printf %%ws, pTCHAR1: %ws\n", pTCHAR1);//wprintf(L"pTCHAR1: %s\n", pTCHAR1);wprintf(_T("wprintf %%s, pTCHAR1: %s\n"), pTCHAR1);printf("\n");std::wcout << "std::wcout, wstring1: " << wstring1.c_str() << std::endl;printf("printf %%ls, wstring1: %ls\n", wstring1.c_str());printf("printf %%ws, wstring1: %ws\n", wstring1.c_str());wprintf(_T("wprintf %%s, wstring1: %s\n"), wstring1.c_str());/* Output:
std::wcout, pTCHAR1: 中文abc
printf %ls, pTCHAR1: 中文abc
printf %ws, pTCHAR1: 中文abc
wprintf %s, pTCHAR1: 中文abcstd::wcout, wstring1: 中文abc
printf %ls, wstring1: 中文abc
printf %ws, wstring1: 中文abc
wprintf %s, wstring1: 中文abc*/
}
TCHAR转为wstring时,因为TCHAR是依据UNICODE宏在编译时确定的,所以,到底是wstring,还是string,也要在编译时确定,,,这种写法,不知道是不是我有代码洁癖,感觉很扯淡~
然后,貌似还只能这么写~
参考:
1.
《cannot convert from 'TCHAR [260]' to 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>》:
http://www.cplusplus.com/forum/general/234191/
2.
《MultiByteToWideChar function (stringapiset.h)》:
https://docs.microsoft.com/zh-cn/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar?redirectedfrom=MSDN
3.
《win32控制台输出带中文Unicode字符串》:
https://blog.csdn.net/zhaogang1993/article/details/52261249?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.control
4.
《setlocale, _wsetlocale》:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?view=msvc-160
不要使用wstring相关推荐
- cocos2d-x开发中wstring和string的转换
cocos2d-x开发中汉字操作技术是必须要面对的,其中可能要用到wstring和string的相互转换.我在网络上搜索到的能够应用于android 和WIN32(其他的平台没有试)环境下的汉字的可选 ...
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....转换
wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString.... #include <strin ...
- PInvoke复习之深入理解char*与wchar_t*与string以及wstring之间的相互转换
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下 #ifndef USE_H_ #define USE_H_ #i ...
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...
转载:http://blog.csdn.net/chocolateconanlan/article/details/4058868 wchar_t*,wchar_t,wchat_t数组,char,ch ...
- wstring与string相互转换
#include <string> #include <locale.h> // 需包含locale.string头文件.使用setlocale函数. std::wstring ...
- string、wstring、cstring、 char、 tchar、int转换方法
1.string转wstring ? 1 2 3 4 5 6 7 wstring s2ws(const string& s) { _bstr_t t = s.c_str(); wchar_ ...
- boost::sort模块实现spreadsort wstring 排序示例
boost::sort模块实现spreadsort wstring 排序示例 实现功能 C++实现代码 实现功能 boost::sort模块实现spreadsort wstring 排序示例 C++实 ...
- 几种C++ std::string和std::wstring相互转换的转换方法
第一种方法:调用WideCharToMultiByte()和MultiByteToWideChar(),代码如下(关于详细的解释,可以参考<windows核心编程>): #include ...
- std::wstring 转为 QString
/*! Convert an std::wstring to a QString */ QString CHelper::stdWToQString(const std::wstring &s ...
- 利用boost做string到wstring转换,以及字符集转换
#include <boost/locale.hpp> int _tmain(int argc, _TCHAR* argv[]) {//std::locale::global(std::l ...
最新文章
- 直播回顾 | 数据驱动「产品迭代」的三大场景详解
- 创客更新装备 动态规划
- Spring Environment
- Git submodule的使用
- 拿下“[warn] (OS 64)指定的网络名不再可用”错误
- 给自己的网址加上https,添加ssl证书(springboot项目)
- 这些标点符号你真的会输入吗
- 计算机视觉目标检测算法总结1——简介
- python 类 字典_python基础类型—字典
- python剑指offer剪绳子
- struts1.x 标签库
- 百度金融与农业银行战略合作,AI +金融的开放故事讲得好吗?
- svn server启动报错:The HTTP service failed to start
- 店铺进销存管理系统源码
- 迷你迅雷,IE下载加速补丁(转)
- 简单的微信使用技巧,你需要掌握的技巧
- 红帽子Linux7安装Oracle,RedHat Enterprise Linux7.0安装Oracle 12C
- 102 613 SWP协议学习笔记--数据链路层
- LeetCode知识点总结 - 884
- win10时间不准_【装机教程】超详细WIN10系统安装教程,官方ISO直装与PE两种方法教程...