gpt4 book ai didi

perl - 在 Perl/Gtk 中显示 CP437 ('extended ascii' )

转载 作者:行者123 更新时间:2023-12-04 14:08:48 28 4
gpt4 key购买 nike

有什么方法可以在 Gtk2::TextView 中显示老式的扩展 ASCII(cp437)? (谷歌建议没有答案。)

如果有一些方法可以更改 GTK 小部件使用的字符集,我找不到它。

或者也许有必要使用 Perl 的 Encode 模块,就像我在下面的脚本中尝试的那样,但这也不起作用。

#!/usr/bin/perl
# Display ASCII

use strict;
use diagnostics;
use warnings;
use Encode;

use Glib qw(TRUE FALSE);
use Gtk2 '-init';

# Open a Gtk2 window, with a Gtk2::TextView to display text
my $window = Gtk2::Window->new('toplevel');
$window->set_title('Extended ASCII viewer');
$window->set_position('center');
$window->set_default_size(600, 400);
$window->signal_connect('delete-event' => sub {

Gtk2->main_quit();
exit;
});

my $scrollWin = Gtk2::ScrolledWindow->new(undef, undef);
$window->add($scrollWin);
$scrollWin->set_policy('automatic', 'automatic');
$scrollWin->set_border_width(0);

my $textView = Gtk2::TextView->new;
$scrollWin->add_with_viewport($textView);
$textView->can_focus(FALSE);
$textView->set_wrap_mode('word-char');
$textView->set_justification('left');
my $buffer = $textView->get_buffer();

$window->show_all();

# In cp437, this is a series of accented A characters
my $string = chr (131) . chr (132) . chr (133) . chr (134);

# Display plain text
$buffer->insert_with_tags_by_name($buffer->get_end_iter(), $string . "\n");

# Display UTF-8 text
my $utfString = encode('utf8', $string);
$buffer->insert_with_tags_by_name($buffer->get_end_iter(), $utfString . "\n");

# Display cp437
my $cpString = decode ('cp437', $string);
my $utfString2 = encode('utf-8', $cpString);
$buffer->insert_with_tags_by_name($buffer->get_end_iter(), $utfString2 . "\n");

# Other suggestion
my $otherString = encode("utf-8", decode ("cp437", $string));
$buffer->insert_with_tags_by_name($buffer->get_end_iter(), $otherString . "\n");

# Directly decode a hex character (as suggested)
my $hexString = encode("utf-8", decode("cp437", "\xBA"));
$buffer->insert_with_tags_by_name($buffer->get_end_iter(), $hexString . "\n");

Gtk2->main();

enter image description here

最佳答案

Gtk2.pm 需要 Perl 字符串 ( Encode::decode(...) ),它们在内部存储为 UTF-8。

如果你给它一个字节串( Encode::encode(...) ),它会尝试将它显示为 latin1。

# In cp437, this is a series of accented A characters
my $string = chr (131) . chr (132) . chr (133) . chr (134);

my $perlString = decode ('cp437', $string);

$buffer->insert_with_tags_by_name($buffer->get_end_iter(), $perlString . "\n\n");


my $charmap = join("", map chr, 128..255);
$charmap =~ s!.{16}\K!\n!g;
$perlString = decode ('cp437', $charmap);

$buffer->insert_with_tags_by_name($buffer->get_end_iter(), $perlString . "\n");

Gtk2->main();

生成的屏幕截图 :

enter image description here

关于perl - 在 Perl/Gtk 中显示 CP437 ('extended ascii' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49737046/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com