Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Unicode codepoints as characters in C?

I have an array of uint32_t elements that each store a codepoint for a non-latin Unicode character. How do I print them on the console or store them in a file as UTF-8 encoded characters? I understand that they may fail to render properly on a console, but they should display fine if I open them in a compatible editor.

I have tried using wprintf(L"%lc", UINT32_T_VARIABLE), and fwprintf(FILE_STREAM, L"%lc", UINT32_T_VARIABLE) but to no avail.

like image 867
hazrmard Avatar asked Oct 18 '22 05:10

hazrmard


1 Answers

You must first select the proper locale with:

#include <locale.h>

setlocale(LC_ALL, "C.UTF-8");

or

setlocale(LC_ALL, "en_US.UTF-8");

And then use printf or fprintf with the %lc format:

printf("%lc", UINT32_T_VARIABLE);

This will work only for Unicode code points small enough to fit in a wchar_t. For a more complete and portable solution, you may nee to implement the Unicode to UTF-8 conversion yourself, which is not very difficult.

like image 113
chqrlie Avatar answered Nov 01 '22 13:11

chqrlie