[Perl][小工具]显示/查询汉字的各种编码值

There's more than one way to do it!
https://metacpan.org http://perlmonks.org
回复
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

[Perl][小工具]显示/查询汉字的各种编码值

帖子 523066680 »

编辑:523066680

显示剪切板中(或者通过参数传递)汉字的各种编码码值(UTF8, UTF16-LE, UTF16-BE, GBK, BIG5)
use Encode;
use IO::Handle;
use Win32::Clipboard;
STDOUT->autoflush(1);

system("");
my $clip;
my $cliptext;
my $inp;

$clip = Win32::Clipboard();

#参数优先
if ( defined $ARGV[0] )
{
func($ARGV[0], "gbk");
print "From argument\n";
}
elsif ( $cliptext = $clip->GetAs(CF_UNICODETEXT) )
{
func( $cliptext, "utf-16le" );
print "\nFrom clipboard";
}
else
{
$str = "";
print "input:";
$inp = <STDIN> or die "$!"; #在 sublime text 终端无法输入
chomp $inp;
func($inp, "gbk");
}
exit;

sub func
{
my ($str, $dec) = (shift, shift);
my ($nstr, $ustr);

$ustr = decode( $dec, $str );

for my $enc ('gbk', 'big5', 'utf8', 'utf16-le', 'utf16-be')
{
$nstr = encode($enc, $ustr);
$nstr=~s/(.{1})/sprintf("%x ", ord($1))/egs;
print $nstr,"\t$enc\n\n";
}

$ustr=~s/(.{1})/sprintf("%d ", ord($1))/egs;
print $ustr,"\tUnicode\n";
}

sub xcode
{
$_[1]='x' if (not defined $_[1]);
for my $v ( split(//,$_[0]) ) {
print sprintf ("%l$_[1] ",ord($v));
}
print "\n\n";
}
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 2 访客