简体   繁体   English

如何在 C(Linux utf8 终端)中打印“盒子抽屉”Unicode 字符?

[英]How to print “box drawers” Unicode characters in C (Linux utf8 terminal)?

I'm trying to display Unicode characters from (Box Drawing Range: 2500–257F).我正在尝试显示来自 (Box Drawing Range: 2500–257F) 的 Unicode 字符。 It's supposed to be standard utf8 (The Unicode Standard, Version 6.2).它应该是标准的 utf8(Unicode 标准,版本 6.2)。 I'm simply unable to do it.我根本做不到。

I first tried to use the good old ASCII characters but the Linux terminal displays in utf8 and there is no conversion (symbol ?) displayed in place.我首先尝试使用旧的 ASCII 字符,但 Linux 终端显示为 utf8,并且没有显示转换(符号?)。

Could anyone answer these questions:任何人都可以回答这些问题:

  • How to encode a unicode character in a C variable ( style wchar_t )?如何在 C 变量( style wchar_t )中编码 unicode 字符?
  • How to use the escape sequence such as 0x or 0o (hex, oct) for Unicode? Unicode 如何使用转义序列,例如 0x 或 0o(十六进制,八进制)?

I know U+ but it seems it didn't work.我知道 U+ 但它似乎不起作用。

setlocale(LC_ALL,"");
short a = 0x2500, b = 0x2501;
wchar_t ac = a;
wchar_t bc = b;
wprintf(L"%c%c\n", ac, bc);
exit(0);

I know that the results are related to the font used, but I use a utf8 font ( http://www.unicode.org/charts/fonts.html ) and codes from 2500 to 257F must be displayed... Actually they aren't.我知道结果与使用的字体有关,但我使用的是 utf8 字体( http://www.unicode.org/charts/fonts.html )并且必须显示 2500 到 257F 的代码......实际上它们是'不。

Thanks for your help in advance...提前感谢您的帮助...

[EDIT LATELY] The issue is solved since... and I found how to use wprintf() with %lc instead of %c... and deeper. [最近编辑] 问题已解决,因为...我发现如何使用 wprintf() 和 %lc 而不是 %c... 更深入。 Now those bow drawers are part of my student "tools" library to make the console programming learning a little more coloured.现在那些弓形抽屉是我学生“工具”库的一部分,使控制台编程学习更加丰富多彩。

Use a Cstring containing the bytes for the utf-8 versions of those characters.使用包含这些字符的 utf-8 版本的字节的 Cstring。 If You print that Cstring, it will print that character.如果您打印该 Cstring,它将打印该字符。

example for Your two characters:您的两个字符的示例:

#include <stdio.h>

int main (int argc, char *argv[])
{
    char block1[] = { 0xe2, 0x94, 0x80, '\0' };
    char block2[] = { 0xe2, 0x94, 0x81, '\0' };
    printf("%s%s\n", block1, block2);
    return 0;
}

prints ─━ for me.打印─━给我。

Also, if You'd print a Cstring containing uft-8 character bytes somewhere in it, it would print those characters without problems.此外,如果您在其中某处打印包含 uft-8 个字符字节的 Cstring,它将毫无问题地打印这些字符。 /* assuming You use gcc */ And IIRC gcc uses utf-8 internally anyway. /* 假设您使用 gcc */ 而且 IIRC gcc 无论如何在内部使用 utf-8。

EDIT: Your question changed a bit while I was writing this.编辑:在我写这篇文章时,你的问题有所改变。 And my answer is less relevant now.我的回答现在不太重要了。 But from Your symptoms - if You see one ?但是从你的症状来看——如果你看到一个? for each character You expect, I'd say Your terminal font might be missing the glyphs required for those characters.对于您期望的每个字符,我会说您的终端字体可能缺少这些字符所需的字形。

That depends on what you call "terminal".这取决于你所说的“终端”。

The linux console uses various hacks to display unicode but in reality its font is limited to 512 symbols IIRC so it can't really display the whole unicode range and what it can display depends on the font loaded (this may change in the future). linux 控制台使用各种技巧来显示 unicode,但实际上它的字体仅限于 512 个符号 IIRC,因此它不能真正显示整个 unicode 范围,它可以显示的内容取决于加载的字体(这可能会在未来改变)。

Windows terminals used to access Linux are usually braindamaged one way or another unicode-wise.用于访问 Linux 的 Windows 终端通常会以一种或另一种 unicode 方式受到脑损伤。

Physical terminals are usually worse and only operate in ascii-land物理终端通常更糟,只能在 ascii-land 中运行

Linux GUI terminals (such as gnome-terminal) can pretty much display everything as long as you have the corresponding fonts.只要您有相应的字体,Linux GUI 终端(例如 gnome-terminal)几乎可以显示所有内容。

Are you sure you don't want to use ncurses instead of writing your own terminal widgets?您确定不想使用 ncurses 而不是编写自己的终端小部件吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM