D
D
Daniil Demidko2016-03-04 13:31:09
C++ / C#
Daniil Demidko, 2016-03-04 13:31:09

How to make your own function for converting from UTF8 to ANSI?

I always created sources in win1251, and used setlocale(LC_ALL, ".866")wcout to output Cyrillic. Then I decided that it was not kosher and started creating sources in utf8. Everything was fine until I again ran into the output of the Cyrillic alphabet.
The old version with setlocale no longer worked and I decided to write my own function for converting Cyrillic characters into a console-readable form.

void SetRusChar(char &symbol)
{
    if(!symbol)
    {
        return;
    }
    /// "Разрыв" кириллицы в таблице символов ANSI
    const int min=-128;
    const int max=-96;
    if(symbol>min&&symbol<max)
    {
        const int addOffset=-112;
        symbol-=addOffset;
    }
    const int offset=16;
    symbol-=offset;
}
std::string GetRusString(std::string line)
{
    const char bigWaste=-48;
    const char littleWaste=-47;
    for(std::string::iterator i=line.begin(); i<line.end(); ++i)
    {
        if(*i==bigWaste||*i==littleWaste)
        {
            line.erase(i);
        }
        SetRusChar(*i);
    }
    return line;
}

It turned out very poorly - it skips some characters.
Then I realized that one cannot do without understanding the ANSI and UTF8 encodings and their device.
How to make a normal function to convert from UTF8 to ANSI? Can you explain in more detail? Are there any ready-made functions? In winApi for example?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2016-03-04
@petermzg

WideCharToMultiByte

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question