C++:需要在控制台输出汉字时(unicode),要记得locale设置 (即系统区域设置)

什么是locale?

Locale class
locale object encapsulates a set of features that are culture-specific, which can be used by programs to enhance international portability (see header  <locale> for more info).

On construction of a  locale object, the localization engine initializes all the  facets associated with it (if necessary) and makes them available to the program.

locale objects are generally constructed from a name (generally, the same as with the  <clocale> function  setlocale), or from another  locale object. They can also mix facets from more than one  locale object.

Every program has a single  locale object which is its  global locale. On start, this is the  classic locale, but this can be changed by calling  locale::global. This  global locale is selected by all  default-constructed  locale objects.

The  global locale also affects the C locale (see function  setlocale): When a new named global locale is set with  locale::global, the C locale is also modified.

locale objects can be used to access their associated facets in order to use their formatting features. They can also be imbued individually to specific stream objects (such as  cincout or a  file stream) by calling the stream's  imbuemember function.

See header  <locale> for more information on locales and facets.

在C/C++程序中,通过设置locale(即系统区域设置,即国家或地区设置)来决定程序所使用的当前语言编码、日期格式、数字格式及其它与区域有关的设置, 如果没有设置locale,可能无法输出中文字符。

locale设置在程序开始时进行如下设置即可。

locale::global(locale(""));

这个设置,对wcout,wprintf的输出都有效。

    wcout.imbue(locale(""));   

这个设置只对wcout有效。

Unicode字符输出的几种方式:

      std::wcout<< lpString<<std::endl;

      wprintf(L"%s\n",lpString);  

      std::cout<<WRITE<<std::endl;

printf("%s\n",WRITE);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值