简体   繁体   English

output c 语言中的一个 unicode 字符

[英]output a unicode character in c language

I tried to output an airplane unicode character in the C language, but I got nothing on the console.我尝试用 C 语言输入 output an airplane unicode 字符,但控制台上什么也没有。

Here is my code:这是我的代码:

#include <stdio.h>
#include <locale.h>

int main() {
    setlocale(LC_CTYPE, setlocale(LC_ALL, ""));
    wprintf(L"🛧");

    return 0;
}

ignore the extra headers, which serve for other purposes.忽略用于其他目的的额外标头。

I expected我期望to be shown on the console, but nothing appeared.显示在控制台上,但没有出现。 I have tried to output some other languages in wprint such as Japanese and Chinese: those just worked fine.我试过 output wprint 中的一些其他语言,例如日语和中文:这些都很好用。 But many other glyphs like airplanes in other shapes and most of the arrows can't work, either.但是许多其他字形,如其他形状的飞机和大多数箭头也不起作用。 just nothing is printed.只是没有打印任何东西。

The first issue is printing the correct character.第一个问题是打印正确的字符。 L"" denotes a wide character string. L"" 表示宽字符串。 Whether that is 16bit wide (Windows) or 32bit wide (Linux) is implementation-defined.是 16 位宽 (Windows) 还是 32 位宽 (Linux) 是实现定义的。 Whether it is Unicode-encoded is locale-dependent.它是否是 Unicode 编码取决于语言环境。

Whether your compiler supports Unicode characters in source is (AFAIK) still implementation-defined.您的编译器是否支持源代码中的 Unicode 个字符(AFAIK)仍然是实现定义的。

Whether your output medium (console in your case) supports wide-character / Unicode strings is depending on that output medium.您的 output 介质(在您的情况下是控制台)是否支持宽字符/ Unicode 字符串取决于该 output 介质。

Whether the font you configured for that output medium supports the character(s) you want to print is depending on the font.您为该 output 介质配置的字体是否支持您要打印的字符取决于字体。

If you want to use Unicode specifically, you should use char8_t[] (UTF-8) or char32_t[] (UTF-32) -- char16_t / UTF-16 also exists if that is what your output medium supports -- and specify characters using \u.... or \U........ character escapes.如果你想专门使用Unicode ,你应该使用 char8_t[] (UTF-8) 或 char32_t[] (UTF-32)——如果你的 output 介质支持的话,char16_t/UTF-16 也存在——并指定字符使用 \u.... 或 \U....... 字符转义。 That takes care of the first two paragraphs of my answer.这处理了我回答的前两段。 The next two could explain why you still might not get what you expected.接下来的两个可以解释为什么您仍然可能无法获得预期的结果。

Despite you lack a #include <wchar.h> header, your program works as espected.尽管您缺少#include <wchar.h> header,但您的程序仍按预期运行。

Check if your console has utf support (normally this means capable of receiving utf8 encoding) If not, it's no worth trying to show an unicode output on it, for evident reasons.检查您的控制台是否支持 utf(通常这意味着能够接收 utf8 编码)如果没有,则不值得尝试在其上显示 unicode output,原因显而易见。

In Linux, this requires that you have a locale (check it executing locale in your console) that includes UTF-8 suffix for utf support (depending on your operating system/version, you will need to spell it in uppercase or lowercase, depending on how the locales have been compiled in your platform)在 Linux 中,这需要您有一个语言环境(检查它在您的控制台中执行locale ),其中包括UTF-8后缀以支持 utf(取决于您的操作系统/版本,您需要将其拼写为大写或小写,具体取决于语言环境已在您的平台中编译)

If you are using Windows, I'm afraid the windows console has no Unicode support (or it has since short ago) so the problem is not in the program (as I say, works perfect) but in the output device.如果您使用的是 Windows,恐怕 windows 控制台没有 Unicode 支持(或者不久前就有),所以问题不在程序中(正如我所说,工作完美),但在 output 设备中。

Things you can still try:您仍然可以尝试的事情:

  • set a locale listed in locale -a by executing the following sequence, then run the program to adjust a proper locale:通过执行以下序列locale -a中列出的语言环境,然后运行程序以调整合适的语言环境:
export LANG="es_ES.UTF-8"

(as I say, that locale must appear when you list all locales with the command above, and it must support unicode encodings) (正如我所说,当您使用上面的命令列出所有语言环境时,该语言环境必须出现,并且它必须支持 unicode 编码)

  • ensure that your console supports the locale you are using.确保您的控制台支持您正在使用的语言环境。
  • redirect the output of your program to a file, and edit that file with a binary editor/viewer, to show the actual bytes output by the program.将程序的 output 重定向到一个文件,并使用二进制编辑器/查看器编辑该文件,以显示程序的实际字节 output。 This will allow you to check if the program effectively printed nothing, or if the problem is that the output terminal doesn't hav a little plane to show (this can also be the problem) Not only suffices to say draw a plane , the receiver must know how to draw it.这将允许您检查程序是否有效打印任何内容,或者问题是 output 终端没有显示小平面(这也可能是问题所在)不仅可以说画一个平面,接收器必须知道如何绘制它。
  • Double check that you have included in your locale one that specifies to use unicode characters (whatever encoding is supported on your system) Even if you are in USA, you need a locale that includes Unicode support, to support non-ASCII characters.仔细检查您的区域设置是否包含指定使用 unicode 字符的区域设置(无论您的系统支持什么编码)即使您在美国,您也需要一个包含 Unicode 支持的区域设置,以支持非 ASCII 字符。

On windows with Visual Studio 2022 this works for me:在带有 Visual Studio 2022 的 windows 上,这对我有用:

#include <Windows.h>
#include <stdio.h>

int main()
{
    SetConsoleOutputCP(CP_UTF8);
    puts(u8"🛧😊");
}

Note the file neds to be saved using UTF-8 encoding.请注意,文件需要使用 UTF-8 编码保存。

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

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