R
R
romangostilo2020-09-23 18:19:33
Apple Xcode
romangostilo, 2020-09-23 18:19:33

How to put Russian character into wchar_t variable in Xcode?

The bottom line is this: the entire group at the university works on Windows, and I work on macOS, the teacher threw off the laboratory work .cpp in ANSI encoding, which, as I understand it, is not in macOS. And one line of code constantly screams an error

wchar_t cw = L'Ф'; // illegal character encoding in character literal

Tried converting file to UTF-8, Cyrillic (Windows) and Cyrillic (macOS). Nothing happened.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Dubrovin, 2020-09-23
@romangostilo

No, this code example is for Windows. Windows uses UTF-16 encoding, for which C under Windows uses wchar, and a Cyrillic character can be specified as a separate wchar. But this will not work with all Unicode characters, because some require more than 1 wchar and don't need to do that. In the general case, for UTF encodings, it is not possible to cram 1 character into one variable, even a string is needed to store one character, so this is an example of bad, not universal code. L for specifying non-ASCII constants is specific to the IDE and compilers for Windows (mainly Visual Studio).
MacOS now mostly uses UTF-8 encoding and a Cyrillic character does not fit into 1 UTF-16 octet. There is no way in C to represent a single UTF-8 character in the general case, because it can also have a variable length, a Cyrillic character can be represented as 2 char characters, they can be stored as a string. It is possible to put them into 1 16-bit variable, but it is not necessary, because in UTF-8, characters can span 1,2,3,4 octets (and potentially more) and using non-8bit types for UTF-8 is impractical. You should rewrite the code to take into account the specifics of UTF encodings and use a string even to store a single character. Such code can potentially be made cross-platform.

M
maaGames, 2020-09-23
@maaGames

convert file from ANSI to UTF-8. There are online converters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question