D
D
Dmitry2014-08-29 22:26:36
C++ / C#
Dmitry, 2014-08-29 22:26:36

C++ how to change the encoding of a string?

C++. There is a need to work with specific characters of string type variables.
The variable may contain letters of the Russian alphabet. I realized that they take not one (like English) but two bytes.
For example, you need to print a letter from a string:

#include <QCoreApplication>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    string s = "Абракадабра";
    cout << s[3] << endl;

    return a.exec();
}

It is logical that a question mark is displayed.
That is, you need a string with a single-byte encoding.
How can a string be converted for such a task?
On the net I met documentation, but for me it was not very clear. I would like an explanation with simple examples.
Used by QT Creator on Ubuntu Linux.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Burov, 2014-08-29
@1diem

If you are using Qt, then use QString.

M
Maxim, 2014-08-30
@1kachan

#include <QString>
#include <QDebug>
int main(int argc, char *argv[])
{
    QString str = QString::fromUtf8("Азазаза");
    qDebug() << str.length();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question