Reputation: 442
How to write utf-8 characters to a file using c++?
For example,
utf-8 value=0xc389
If I write this into a file in binary mode , I have to see characters.
so far I have tried the following
unsigned short array[4]={0xc3,0x89,0xc3,0x8a};
std::ofstream file;
file.open("1.txt", std::ios_base::binary);
file.write(reinterpret_cast<char const*>(array),sizeof(array));
But it takes 0xc3
as single character and it prints its equivalent character of 0xc3
unicode
value.
Help me how to write the utf-8
value to file using c++
in binary mode
Upvotes: 0
Views: 8017
Reputation: 9395
Why are you using short
array when char
array will solve your purpose?
First of all, in UTF-8, you write byte by byte. If your sequence is a valid UTF-8 byte sequence, then write them byte by byte.
If it is unicode character, first convert your character stream to UTF-8 character as characters beyond 0x7f will be treated differently.
//I assume this a valid UTF-8 sequence.
unsigned char array[4]={0xc3,0x89,0xc3,0x8a};
std::ofstream file;
file.open("1.txt", std::ios_base::binary);
file.write(reinterpret_cast<char const*>(array),sizeof(array));
In case, array is not a UTF-8 sequence but some character, you can use iconv
on linux to convert to UTF-8.
Upvotes: 3