如何在C中使用Unicode区块?

发布于 2025-01-18 14:26:39 字数 600 浏览 3 评论 0原文

我想在我的 C 程序中使用 unicode 块将它们显示在控制台中,例如 ▇、░ 等。但是,每当我尝试对 unicode 字符使用转义序列时,我只会收到奇怪的字母,例如:

printf("/u259A"); //259A is the unicode for ▚
Output: ÔûÜ

我查找了如何包含 unicode 字符,然后尝试使用 wchar_t:

#include <locale.h>
#include <stdio.h>
int main(int argc, char const *argv[]) {
  setlocale(LC_ALL, "");
  wchar_t c = "\u259A";
  printf("%c",c);
  return 0;
}

但这只给了我 作为输出而不是▚。删除 setlocale() 会给我一个空白输出。我不知道从现在开始该做什么。我唯一看到的是使用 printf("\xB2"); ,它给了你。但我不明白 B2 来自哪里或它代表什么。

I want to use unicode blocks in my C program to display them in the console like ▇, ░ and so on. However, whenever I try to use the escape sequence for unicode characters, I only get weird letters like:

printf("/u259A"); //259A is the unicode for ▚
Output: ÔûÜ

I looked up how to include unicode charactes then tried to use wchar_t:

#include <locale.h>
#include <stdio.h>
int main(int argc, char const *argv[]) {
  setlocale(LC_ALL, "");
  wchar_t c = "\u259A";
  printf("%c",c);
  return 0;
}

but that only gave me as the output instead of ▚. Removing setlocale() would give me a blank output. I dont know what do to from this point on. The only thing I saw was using printf("\xB2"); which gave you . But I dont understand where the B2 comes from or what it stands for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2025-01-25 14:26:39

因此,对我有用的是:

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main(int argc, char const *argv[]) {
  _setmode(_fileno(stdout), _O_U16TEXT);
  wprintf(L"\x2590 \x2554 \x258c \x2592"); //Output : ▐ ╔ ▌ ▒
  return 0;
}

函数_SETMODE()显然是用于在U16文本编码上设置控制台。 wprintf()允许您打印宽字符(unicode同样)。 l“”在字符串向编译器指示之前,以下字符串是Unicode字符串。感谢大家的时间和答案!

So what worked for me was the following:

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main(int argc, char const *argv[]) {
  _setmode(_fileno(stdout), _O_U16TEXT);
  wprintf(L"\x2590 \x2554 \x258c \x2592"); //Output : ▐ ╔ ▌ ▒
  return 0;
}

the function _setmode() is apparently for setting the console on u16 text encoding. wprintf() allows you to print wide characters (unicode aswell). The L"" before the string indicates to the compiler, that the following string is a unicode string. Thanks to everyone for their time and answers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文