F
F
Ffgfhgh2018-01-28 20:30:43
linux
Ffgfhgh, 2018-01-28 20:30:43

How to correctly convert a file with utf-32 encoding to utf-8 using the UTF8-CPP library?

I have a file in utf-32 encoding and I need to convert it to utf-8, here is my program code:

#include "source/utf8.h"
using namespace std;

int main(int argc, char** argv)
{
    const char* sourceFilePath = "/home/user/utf32.txt";

    ifstream sourceFile(sourceFilePath, std::ifstream::binary);
    ofstream outFile("/home/user/utf8result.txt");

    vector<unsigned char> utf8result;
       utf8::utf32to8(std::istreambuf_iterator<char>(sourceFile),
                      std::istreambuf_iterator<char>(),
                      back_inserter(utf8result));
       outFile.write((char*)&utf8result[0], utf8result.size());

    return 0;
}

There are no errors, but for some reason the new file is still encoded in utf-32, this can be seen from the fact that the sizes of the received and the old file are the same, I also looked in the HEX Editor - it also shows that each character in utf8result.txt takes 4 bytes.
The utf8result.txt file must be in utf-8 encoding and must be 24 bytes long. Why is it so? I used this simple but famous library:
utfcpp.sourceforge.net
Please help me. I've been trying all day to find the right solution, although it shouldn't be hard.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Moseychuk, 2018-01-29
@fshp

utf8::utf32to8(std::istreambuf_iterator<char32_t>(sourceFile),
                      std::istreambuf_iterator<char32_t>(),
                      back_inserter(utf8result));

X
xibir, 2018-01-29
@xibir

Why use a third party library? Linux has an iconv function (from glibc) that encodes utf and other encodings

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question