0

In my code I tried to create massive of 4 bite chars, where every char contain a Cyrillic letter.

  wchar_t OUT_STRING[4] = { L'т',L'л',L'о',L'р' };

All in normal with this and I have expected output. It's only test, in real I need to convert element from string to the same type like in OUT_STRING; I tried to use something like this:

 wchar_t OUT_STRING[4] = { (wchar_t)'т',L'л',L'о',L'р' };

But it didn't work and in output I have a rectangle.

  • 2
    "_L before the char change type of it on wchar_t_" It doesn't _change type_. It is defined that `'a'` is a `char` literal, while `L'a'` is a `wchar_t` literal. To read more about various type of character literals, go [here](https://en.cppreference.com/w/cpp/language/character_literal). – Algirdas Preidžius Sep 24 '18 at 14:38
  • 1
    `x` is undefined initially. You don't assign `x` an initial value anywhere. try `static int32_t x = 0`; – Wyck Sep 24 '18 at 14:52
  • Ok, but how to define 'т' on this style: (wchar_t)'т' – Черный Камень Sep 24 '18 at 14:54
  • `L'т'` is a wchar_t literal character т – Wyck Sep 24 '18 at 14:55
  • @ЧерныйКамень Different kinds of literals have different prefix or suffix. `L` does not perform a cast, it identifies the next sequence as a [wide character literal](https://en.cppreference.com/w/cpp/language/character_literal). – François Andrieux Sep 24 '18 at 14:57
  • How to do this with variable? – Черный Камень Sep 24 '18 at 15:05
  • I *think* you want to pass in a string using std::string (presumably in UTF-8 encoding?) and process it one character at a time. You could convert the input string to UTF-16 and easily process each character one at a time. By putting each character into a wchar_t[2] { ch, 0 }. But looks like you'll need a full example. Is that what you want? UTF-8 to UTF-16 example here: http://www.cplusplus.com/reference/codecvt/codecvt_utf8_utf16/ – Wyck Sep 24 '18 at 15:10
  • @ЧерныйКамень "_How to do this with variable?_" You _convert_ the value from `char` to `wchar_t`. Casting is **not** conversion. Related: https://stackoverflow.com/questions/8032080/how-to-convert-char-to-wchar-t , https://stackoverflow.com/questions/3019977/convert-wchar-t-to-char – Algirdas Preidžius Sep 24 '18 at 15:13
  • @Wyck "*You could convert the input string to UTF-16 and easily process each character one at a time*" - That would not work for supplementary characters outside the BMP, which require 2 UTF-16 codeunits per character (surrogate pairs). So you are right back in the same boat of having to handle variable-length encodings. If you really want to process one Unicode character at a time, you would need to convert to UTF-32 instead. – Remy Lebeau Sep 24 '18 at 18:25

1 Answers1

0

I think you want to pass in a string using std::string in UTF-8 encoding and process it one character at a time, each time converting the single character to a wide character string of length 1 so that you can pass it to TTF_SizeUNICODE, and TTF_RenderUNICODE_Blended.

I will demonstrate the relevant string conversion code.

Here is a test function that expects a null-terminated wide character string with just one character in it. The body of main shows how to convert a UTF-8 string to UTF-16 (using codecvt_utf8_utf16) and how to convert a single character to a string (using std::wstring(1, ch))

#include <string>
#include <codecvt> 
#include <iostream>

void test(const wchar_t* str) {
    std::cout << "Number of characters in string: " << wcslen(str) << std::endl;

    for (const wchar_t* ch = str; *ch; ++ch) {
        std::cout << ((int)*ch) << std::endl;
    }
}

int main() {
    std::string input = u8"тлор";
    for (wchar_t ch : std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().from_bytes(input)) {
        std::wstring string_with_just_one_character(1, ch);

        test(string_with_just_one_character.c_str());
    }
    return 0;
}
Wyck
  • 10,311
  • 6
  • 39
  • 60
  • This will break for supplementary characters outside the BMP, which require 2 `wchar_t`s (surrogate pair) to encode them in UTF-16. – Remy Lebeau Sep 24 '18 at 18:27