如何在 Perl 中显示扩展 ASCII 代码字符?

发布于 2024-09-24 19:28:45 字数 123 浏览 2 评论 0原文

alt text

如何显示 192 个字符的符号( └ )珀尔?

alt text

How to display 192 character symbol ( └ ) in perl ?

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

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

发布评论

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

评论(5

我为君王 2024-10-01 19:28:45

你想要的是能够打印unicode,答案就在 perldoc perluiintro

您可以使用 \x{nnnn} 其中 n 是十六进制标识符,或者您可以使用以下名称执行 \N{...}

perl -E 'say "\x{2514}"; use charnames; say "\N{BOX DRAWINGS LIGHT UP AND RIGHT}"'

What you want is to be able to print unicode, and the answer is in perldoc perluniintro.

You can use \x{nnnn} where n is the hex identifier, or you can do \N{...} with the name:

perl -E 'say "\x{2514}"; use charnames; say "\N{BOX DRAWINGS LIGHT UP AND RIGHT}"'
一个人的夜不怕黑 2024-10-01 19:28:45

要准确使用这些代码,您的终端必须支持代码页 437,其中包含框架。或者,您可以使用派生的 CP850 来减少拳击字符。
此类装箱字符也以 Unicode 块元素 的形式存在。 Perl 中所需的字符被标记为 \N{U+2514}。更多详细信息请参见 perlunicode

To use exactly these codes your terminal must support Code Page 437, which contains frames. Alternatively you can use derived CP850 with less boxing characters.
Such boxing characters also exist as Unicode Block Elements. The char which you want in perl is noted as \N{U+2514}. More details in perlunicode

烟若柳尘 2024-10-01 19:28:45

这看起来像代码页 437 编码。 Perl 可能只是输出您给它的字节。您的终端可能需要 UTF8。

因此,您需要将其解码为 Unicode,然后将其重新编码为 UTF-8。

编辑:正确的编码。

That looks like the Code page 437 encoding. Perl is probably just outputting bytes that you give it. And your terminal is probably expecting UTF8.

So you need to decode it to Unicode, then re-encode it in UTF-8.

EDIT: Correct encoding.

东走西顾 2024-10-01 19:28:45

As usual, Jon Skeet nails it: the 192 code is in the "extended ASCII" range. I suggest you follow @Douglas Leeder's advice, but I'm not sure which encoding www.LookupTables.com is giving you; ISO-8859-1 thinks 192 maps to "À", and Mac OS Roman thinks its "¿".

娇妻 2024-10-01 19:28:45

有没有适用于所有角色的解决方案?

用户说他们想使用 latin-1 扩展字符集字符 - 所以让我们尝试一下此块中的示例!因此,如果他们想要 Æ 字符,他们会运行...

print "\x{00C6}";

输出:

完全可测试的在线演示

Perl 中的 TDLR 字符编码模式

那么,等等,刚刚发生了什么?您会注意到调用 UTF-8 的其他方式,例如 char(...)\N{U+...},甚至 unpack(...) 也有同样的问题。没错——问题不在于这些函数,而在于底层的字符抽象层。在这种情况下,您需要在代码中尽早指示该层。

use open qw( :std :encoding(UTF-8) );
print "\x{00C6}";

输出:Æ

现在我可以正确拼写'Ælf'

完整可测试的在线演示

为什么会发生这种情况?

PerlDoc 中有一个关于 chr() 函数的注释< /strong>....

请注意,出于向后兼容性的原因,默认情况下,从 128 到 255(含)的字符在内部不会编码为 UTF-8。

因此,这个特殊的块需要有特殊的use open来指示std编码。

Is there a solution that works on ALL characters?

The user says they wanted to use an latin-1 extended charset character — so let's try an example from this block! So, if they wanted the Æ character, they would run...

print "\x{00C6}";

Output:

Full Testable, Online Demo

TDLR Character Encoding Modes in Perl

So, wait, what just happened there? You'll notice that other ways of invoking UTF-8, such as char(...), \N{U+...}, and even unpack(...) also have the same issue. That's right -- the problem isn't with any of these functions, but an underlying character abstraction layer. In this case, you'll want to indicate this layer early in your code..

use open qw( :std :encoding(UTF-8) );
print "\x{00C6}";

Output: Æ

Now I can spell 'Ælf' correctly!

Full Testable, Online Demo

Why did that happen?

There is a note within the PerlDoc regarding the chr() function....

Note that characters from 128 to 255 (inclusive) are by default internally not encoded as UTF-8 for backward compatibility reasons.

For this reason, this special block needs to have that special use open to indicate std encoding.

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