0

According to this wiki link, the play cards have Unicode of form U+1f0a1. I wanted to create an array in c++ to sore the 52 standard playing cards but I notice this Unicode is longer that 2 bytes.

So my simple example below does not work, how do I store a Unicode character that is longer than 2 bytes?

wchar_t t = '\u1f0a1';
printf("%lc",t);

The above code truncates t to \u1f0a

Siddharth Chabra
  • 448
  • 6
  • 22
  • to print wide character strings use [wprintf](http://www.cplusplus.com/reference/cwchar/wprintf/) – user3366592 Jun 12 '18 at 09:55
  • that is part of the problem... as wchar only stores 2 bytes i am dealing with longer unicode chars – Siddharth Chabra Jun 12 '18 at 09:56
  • For characters that are longer than 2 bytes you could use `char32_t`, but looks like it is hard to print such characters in a console ([see here](https://stackoverflow.com/a/15857922/3366592)) – user3366592 Jun 12 '18 at 10:04
  • 1
    @SiddharthChabra Use `std::wstring` instead of `wchar_t`. On platforms where `wchar_t` is 2 bytes (Windows), encode the `std::wstring` using UTF-16, eg: `std::wstring t = L"\uD83C\uDCA1";` – Remy Lebeau Jun 13 '18 at 20:45

2 Answers2

0

On Windows wchar_t stores a UTF-16 code-unit, you have to store your string as UTF-16 (using a string-literal with prefix) This doesn't help you either since the windows console can only output characters up to 0xFFFF. See this:

How to use unicode characters in Windows command line?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
The Techel
  • 918
  • 8
  • 13
  • Various console replacements can deal with full range of Unicode characters (I'm using ConEmu and it displays the character in question with no problem whatsoever). – n. m. could be an AI Jun 12 '18 at 10:39
0

how do I store a longer that 2 byte unicode character?

you can use char32_t with prefix U, but there's no way to print it to console. Besides, you don't need char32_t at all, utf-16 is enough to encode that character. wchar_t t = L'\u2660', you need the prefix L to specify it's a wide char.


If you are using Windows with Visual C++ compiler, I recommend a way:

  1. Save your source file with utf-8 encoding
  2. set compile parameter /utf-8, reference here.
  3. use a console supports utf-8 encoded like Git Bash to see the result.

enter image description here

Rick
  • 7,007
  • 2
  • 49
  • 79