Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Vietnamese characters in C++?

Tags:

c++

c++11

locale

So I'm currently learning C++11 and I'm getting the hang of it. I want to play around with using a different language and since I'm Vietnamese, I want to make a C++ program that uses Vietnamese characters.

So how can I display Vietnamese characters the same way that English is displayed, which is like this:

cout << "Hello. This is English" << endl; //English
cout << "Chào. Đây là tiếng Việt." << endl; //Vietnamese

I heard that C++ has <locale>. Does it help make the Vietnamese characters appear?

like image 365
Starchipper Avatar asked Jan 14 '17 02:01

Starchipper


People also ask

Is Vietnamese a UTF 8?

A Vietnamese-language file in UTF-8 encoding is roughly 1.2 times larger than a file with same content but encoded using legacy encoding formats (VPS, VISCII, TCVN3, i.e.), for Vietnamese characters (mostly, vowels) in UTF-8 format usually require two to three bytes to represent.

What character encoding is used for Vietnam?

VN, ABC or simply the TCVN encodings, is a set of three closely related Vietnamese national standard character encodings for using the Vietnamese language with computers, developed by the TCVN Technical Committee on Information Technology (TCVN/TC1) and first adopted in 1993 (as TCVN 5712:1993).


2 Answers

You may be running into a problem with your environment. You don't say what platform/environment you are running in, but take the following program:

#include <iostream>
#include <cstdlib>

int main()
{
    std::cout << u8"Chào thế giới!" << std::endl;
    return EXIT_SUCCESS;
}

This yields the following output from iTerm on Mac OS X: Chào thế giới!

With other (non-unicode) environments, using the same code, you may get UTF-8 characters interpreted as ASCII on output. I don't know what the Windows command line will yield, but if you are using an IDE, your IDE may or may not render UTF-8, independently of whether your shell does or doesn't.

Here's a web example.

https://code.sololearn.com/c39N9RN6b4Md/#cpp yields: Chào thế giới!

But http://ideone.com/OkkUZs running exactly the same code yields: Chào thế giới!

It's probably also worth pointing out that in C++ to properly process UTF-8 strings, count "characters", ensure your strings are valid UTF-8, etc. you will likely want to use a Unicode library--working with Unicode is non-trivial.

Personally, I have found both UTFCPP and TinyUTF8 to be excellent libraries - reasonably small, simple and effective.

Hope that helps.

like image 103
U007D Avatar answered Oct 13 '22 13:10

U007D


#include <iostream>
#include <io.h>
#include <fcntl.h>

int main() {
    _setmode(_fileno(stdout), _O_U16TEXT);
     std::wcout << L"Chào. Đây là tiếng Việt.";
}

This is a solution that works for windows. Unfortunately it's not portable to other platforms.

like image 3
ScY Avatar answered Oct 13 '22 11:10

ScY