F
F
Festelo2016-11-12 20:24:16
Qt
Festelo, 2016-11-12 20:24:16

C++QT5: UTF-8, QString, QByteArray, *char and Russian characters. How to change element in *char array?

There is a QString in UTF-8, I need to get an *char array from it, I do this:

QString word = "Слово";
QByteArray ar = word.toUtf8();
char* FirstArray = ar.data();
FirstArray[0] = 'X';
QString word1 = QString::fromUtf8(ar.data());

And word1 becomes equal to Xword, not Xword. How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2016-11-12
@Festelo

In UTF-8 encoding, one character is one to four bytes. Humble yourself, and if you want to change the Russian letter (2 bytes) to English (1 byte), it is better to work in UTF-16 encoding.

word1 = word;
word1[0] = 'X';

or
word1 = QString::fromUtf8(ar.data());
word1[0] = 'X';

And if there is no way, then parse UTF-8, of course.
In general, multibyte encodings are inconvenient for such operations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question