Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #82862

Re: Hello World in Russian

From "Alf P. Steinbach" <alf.p.steinbach@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Hello World in Russian
Date 2022-01-29 11:48 +0100
Organization A noiseless patient Spider
Message-ID <st362k$ham$1@dont-email.me> (permalink)
References <i6GdnUGdpJgzJGn8nZ2dnUU7-KnNnZ2d@giganews.com>

Show all headers | View raw


On 29 Jan 2022 04:51, Joseph Hesse wrote:
> In the following program, I have written "hello world", copied from the 
> iternet, in Russian.  I don't understand Russian but I understand their 
> alphabet is different from the English alphabet.  The program is 
> supposed to output the original message and the characters in the 
> original message.  My question is why are they different?
> Thank you,
> Joe
> 
> $ more Hello.cpp
> #include <iostream>
> using namespace std;
> 
> int main()
> {
>    const wchar_t russian[] = L"Привет мир";  //Russian for "hello, world"
> 
>    wcout << russian << endl;
> 
>    for(const wchar_t &x : russian)
>      wcout << x << L", ";
>    wcout << endl;
> }
> $ g++ -std=c++14 Hello.cpp
> $ ./a.out
> Privet mir                        <== original message
> P, r, i, v, e, t,  , m, i, r, ,   <== wchars in original message
> $

I reproduced your result in Ubuntu.

The reason this happens is:

* The wide streams translate to/from the external byte-oriented encoding.
* With no locale specified the external encoding wcout knows about is 
the one in the default "C" locale, namely pure ASCII.
* It uses a translation where symbols are mapped to phonetically similar 
characters in the result encoding.

Generally it's a pretty ungood idea to use the wide streams. Since they 
don't work in modern Windows (they get confused about the external 
encoding, in all implementations) they now have the direct opposite 
effect of the intended one. Namely, they make the code non-portable.

The modern way to do Russian or whatever output is to assume UTF-8:

     #include <iostream>
     using namespace std;

     auto main() -> int
     {
         const auto& russian = "Привет мир";     // Russian for "hello, 
world".
         cout << russian << endl;
     }

This works when it's built and invoked correctly, but unfortunately 
there's no error when it's not done correctly.

---

The general UTF-8 assumption can however be easily checked for the first 
two point below, about assuming UTF-8

* as the source encoding,
* as the C++ literal constants encoding (the "execution character set"), and
* as the external environment's byte oriented encoding.

The following code statically asserts that the two first points hold:

     #include <iostream>
     using namespace std;

     constexpr auto utf8_is_the_execution_character_set()
         -> bool
     {
         constexpr auto& slashed_o = "ø";
         return (sizeof( slashed_o ) == 3 and slashed_o[0] == '\xC3' and 
slashed_o[1] == '\xB8');
     }

     static_assert(
         utf8_is_the_execution_character_set(),
         "The execution character set must be UTF-8 (e.g. MSVC option 
\"/utf-8\")."
         );

     auto main() -> int
     {
         const auto& russian = "Привет мир";     // Russian for "hello, 
world".
         cout << russian << endl;
     }


You have to ensure the third aspect, that UTF-8 is the external 
encoding, in some way. In Linux it's the default. In Windows it can be 
ensured /for output/ by setting the console window's active codepage to 
65001, e.g. via a command such as `chcp 65001`, or programmatically.

---

Unfortunately, still as of Windows 11 console windows do not support 
input of UTF-8, other than the ASCII subset. :(

You can however use various 3rd party libraries to do portable UTF-8 input.

Those libraries include Boost Nowide and my own still under construction 
library code, currently called Kickstart. At the start of the Boost 
adoption process, when Nowide had been formally adopted but not yet 
released with Boost, the library still got Windows CRLF endings wrong, 
so it's perhaps not the highest quality library in the world. And 
Kickstart has been changing, e.g. filenames and file locations, about 
every time I've looked at it, in spite of my claims that /now/ it's 
pretty stable, so probably neither of them are suitable for commercial code.

The common theme is that these libraries require you to /explicitly/ use 
their UTF-8 input, instead of just overriding the behavior of `std::cin` 
or `stdin`. I believe because there are so many parties, including 
Microsoft, doing that that it gets real messy. I did that once, in an 
intended library, but it turned out that Microsoft changed things, 
introduced new sabotage-like stuff, faster than I could keep up.

And unfortunately the people in charge of the standardization of C++ 
just plain refuse to fix things, such as e.g. an ensured UTF-8 mode of 
the console i/o, and instead insist on re-educating C++ programmers into 
using modern `char8_t` instead of `char`. Like re-educating 
heterosexuals into adopting modern trans-sexual behavior. To this end in 
C++20 they broke all code that made use of critical functionality of 
`std::filesystem::path` (critical for Windows use, that is), and they 
were very much made aware of that and refused to reconsider, so it's 
very political, after 25 years, which means it will never get fixed.


- Alf

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Hello World in Russian Joseph Hesse <joeh@gmail.com> - 2022-01-29 05:51 +0200
  Re: Hello World in Russian James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-28 23:27 -0500
    Re: Hello World in Russian Muttley@dastardlyhq.com - 2022-01-29 10:21 +0000
  Re: Hello World in Russian Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-28 22:42 -0800
    Re: Hello World in Russian David Brown <david.brown@hesbynett.no> - 2022-01-29 13:01 +0100
      Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-29 14:12 +0100
        Re: Hello World in Russian David Brown <david.brown@hesbynett.no> - 2022-01-29 17:34 +0100
      Re: Hello World in Russian James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-30 14:17 -0500
        Re: Hello World in Russian Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-30 20:17 -0800
  Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-29 11:48 +0100
    Re: Hello World in Russian Paavo Helde <eesnimi@osa.pri.ee> - 2022-01-29 17:39 +0200
      Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-30 09:18 +0100
        Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-30 12:12 +0000
          Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-30 14:02 +0100
            Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-30 14:45 +0000
              Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-30 21:53 +0100
                Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-30 21:00 +0000
            Re: Hello World in Russian James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-30 14:02 -0500
          Re: Hello World in Russian Muttley@dastardlyhq.com - 2022-01-31 10:23 +0000
            Re: Hello World in Russian scott@slp53.sl.home (Scott Lurndal) - 2022-01-31 16:11 +0000
              Re: Hello World in Russian Muttley@dastardlyhq.com - 2022-01-31 16:36 +0000
              Re: Hello World in Russian Paavo Helde <eesnimi@osa.pri.ee> - 2022-01-31 20:20 +0200
            Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-31 16:12 +0000
              Re: Hello World in Russian Muttley@dastardlyhq.com - 2022-01-31 16:39 +0000
                Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-31 23:47 +0000
        Re: Hello World in Russian Manfred <noname@add.invalid> - 2022-01-31 16:45 +0100
        Re: Hello World in Russian Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-31 10:10 -0800
          Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-31 19:25 +0100
      Re: Hello World in Russian Marcel Mueller <news.5.maazl@spamgourmet.org> - 2022-01-30 12:09 +0100
        Re: Hello World in Russian Paavo Helde <eesnimi@osa.pri.ee> - 2022-01-30 14:26 +0200
          Re: Hello World in Russian Richard Damon <Richard@Damon-Family.org> - 2022-01-30 08:59 -0500
            Re: Hello World in Russian Paavo Helde <eesnimi@osa.pri.ee> - 2022-01-30 18:21 +0200
              Re: Hello World in Russian Richard Damon <Richard@Damon-Family.org> - 2022-01-30 12:56 -0500
                Re: Hello World in Russian Vir Campestris <vir.campestris@invalid.invalid> - 2022-02-07 21:05 +0000

csiph-web