【问题标题】:Windows-1252 to unicode conversion in perlWindows-1252 到 perl 中的 unicode 转换
【发布时间】:2014-03-11 10:41:10
【问题描述】:

我有 Windows-1251 十六进制格式的 ef(cyrillic) 字符。 值为 0xF4。我想在 perl 中转换和打印字符。 我可以通过 unicode 0x0444 做到这一点。 我正在寻找一种将 0xF4 转换为 0x044 的方法。 我的最终计划是任何编码的任何字符的十六进制值,我应该能够将其转换为 unicode 的十六进制值并最终能够打印它。 但它不工作 以下是我正在使用的代码

#!/usr/bin/perl
use strict;
use utf8;
use Encode qw(decode encode);

binmode(STDOUT, ":utf8");
my $runtime = chr(0x0444);
   print "theta || ".$runtime." ||";
   my $smiley = "\x{0444}";
   print "theta || ".$smiley." ||";
   my $georgian_an  = pack("U", 0x0444);
   print "theta || ".$georgian_an." ||";

  my $hexstr = "0xF4";
  my $num = hex $hexstr;
  print $num;  # printing the hex value
  my $be_num = pack("N", $num);
  $runtime = decode( "cp1252",$be_num);
  print "\n".$runtime."\n"; # i should have got ф here

输出

perl mychar_new.pl
theta || ф ||theta || ф ||theta || ф ||244

ô

【问题讨论】:

    标签: perl unicode


    【解决方案1】:

    输出是正确的——在 CP-1252 中,0xF4 确实是 ô (Wikipedia)。

    您想指定CP-1251

    use Encode 'decode';
    my $cp1251 = "\xF4";
    my $decoded = decode "cp1251", $cp1251;
    print "$decoded\n";
    

    【讨论】:

    • 谢谢阿蒙,这有帮助。让我分享我为什么需要这个。我正在尝试解析一个 rtf 文件,其内容为 ф & 在 vi​​ 中如下所示。 rtf 提到了 unicode cpg1252。 RTF::TEXT::Converter 不起作用,因此使用 RTF::Tokenizer {\rtf1\ansi\ansicpg1252\fromtext \fbidis \deff0{\fonttbl {\f0\fswiss\fcharset0 Arial;} {\f1\fmodern Courier New ;} {\f2\fnil\fcharset2 符号;} {\f3\fmodern\fcharset0 Courier New;} {\f4\fswiss\fcharset204 Arial;}} {\colortbl\red0\green0\blue0;\red0\green0\blue255 ;} \uc1\pard\plain\deftab360 \f0\fs20 \htmlrtf{\f4\fs20\htmlrtf0 \'f4\htmlrtf\f0}\htmlrtf0 \par }
    猜你喜欢
    • 2011-05-20
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 2014-05-06
    • 2021-04-09
    相关资源
    最近更新 更多