如何在C语言中将Unicode代码点打印为字符? [英] How to print Unicode codepoints as characters in C?

查看:94
本文介绍了如何在C语言中将Unicode代码点打印为字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 uint32_t 元素数组,每个元素存储一个非拉丁Unicode字符的代码点.如何在控制台上打印它们或将它们以UTF-8编码字符存储在文件中?我知道它们可能无法在控制台上正确呈现,但是如果我在兼容的编辑器中将其打开,它们应该可以正常显示.

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.

我尝试使用 wprintf(L%lc",UINT32_T_VARIABLE) fwprintf(FILE_STREAM,L%lc",UINT32_T_VARIABLE),但无济于事.

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

推荐答案

您必须首先使用以下选项选择正确的语言环境:

You must first select the proper locale with:

#include <locale.h>

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

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

然后使用%lc 格式的 printf fprintf :

printf("%lc", UINT32_T_VARIABLE);

这仅适用于足够小以适合 wchar_t 的Unicode代码点.对于更完整和可移植的解决方案,您可以自己实现将Unicode转换为UTF-8的操作,这并不是很难.

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.

这篇关于如何在C语言中将Unicode代码点打印为字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆