The processing of Chinese in C + + and the problem of Chinese character garbled (wchar_t)

Source: Internet
Author: User
Tags locale

The processing of Chinese characters in C + +

Today programming language and programming environment with China's development began to support the Chinese language, but the support for the Chinese language is generally flawed, and there are different from the compilation environment, resulting in Chinese in the current C + + has a lot of problems, and many versions of Chinese support is incomplete, take dev-c++ And VS2005, for example, there are a lot of differences between the support for MSDN and the online storytelling in those code.

And what I'm going to talk about is how to apply Chinese in C + +.

First, the text is outside the scope of the general char, so we can not use a single char to store our Chinese characters, so we mostly introduce wchar_t this wide character data type. But in the compilation environment I used is generally defined as wchar_t, which is a recognized definition in the C + + language, his space is the same size as the unsigned short , so there is this internal definition:typedef unsigned short wchar_t, he is a person of.

In dev-c++ we have a lot of methods that are not available, for VS2005, we can define and apply many methods and many library functions are not available in dev-c++. The input and output methods mentioned in MSDN and many web materials, like wcin and Wout, are displayed undefined in dev-c++, which means that dev-c++ does not support these methods. The input and output of simple wide characters are as follows:

#include <iostream>

using namespace std;

int Main ()

{

wchar_t a[3 ];

wcin >> A;

wcout << a << Endl;

return 0;

}

But this can only enter a single kanji character, if more than 2 Chinese words will have overflow error, and in such a way, although we used WCHAR but completely did not highlight our purpose, it is still a Chinese character accounted for two wchar_t units , And we have no way to manipulate the characters inside the character so this is not feasible, but this is the use of C, in C + + WCHAR It has been modified, so that Chinese support is better.

in the C + + in which wchar_t is a language built-in data type, wchar_t the length is determined by the implementation. Now we are officially starting to discuss the issue of support and application of Chinese in our C + + .

C + + is a good language, it is in order to adapt to the development of different locales, it joins a header file called the locale package, which defines the abbreviations of different languages and languages. This is an important aspect of the way we use wchar_t to conduct Chinese in one aspect of the operation. has a significant impact on our input and output.

The first example of our application is this:

#include <iostream>

#include <locale>

using namespace std;

int Main ()

{

Locale Loc ( "chinese-simplified");

Wcin.imbue (Loc);

Wcout.imbue (Loc);

The above three lines of code and setlocale (Lc_all, "CHS") have the same effect.

wchar_t c[4 ];

wcin >> C;

wcout << c <<endl;

return 0;

}

Here I use the door #include <locale> to include this header file, and then generate a locale object, the parameters are only in Chinese, We then set the imbue of our inputs and outputs to changes the role of the locale. So our input and output will become the stored procedures we need in the Chinese text unit. In the example above C[4] can enter 3 Chinese characters, the last one is ' "", so that the initial achieved the desired effect. At the same time we can take out each of the positioning of the Chinese characters, and char the same data processing.




The processing of Chinese in C + + and the problem of Chinese character garbled (wchar_t)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.