【问题标题】:What extended ASCII encoding is this and how can I get ruby to understand it?这是什么扩展的 ASCII 编码,我怎样才能让 ruby​​ 理解它?
【发布时间】:2013-10-01 22:54:28
【问题描述】:

字符0x910x920x930x94应该代表Unicode中的U+2018U+2019U+201cU+201d,或者“开头单引号”、“关闭单引号”、“打开双引号”和“关闭双引号”。我以为是ISO-8859-1,但是当我尝试使用IO.read('file', :encoding=>'ISO-8859-1') 处理文件时,它仍然无法识别这些字符。

如果不是ISO-8859-1 那是什么?如果是,为什么 ruby​​ 不能识别这些字符?

更新:显然这种编码应该是 Windows-1252。但是当我IO.read('file', :encoding=>'Windows-1252') 时,ruby 仍然无法识别这些字符。

更新 2:没关系,Windows-1252 有效。

【问题讨论】:

    标签: ruby-on-rails ruby encoding extended-ascii


    【解决方案1】:

    0x91 是 Unicode 的 \u2018(又名 )的 Windows-1251 表示:

    >> "\x91".force_encoding('windows-1251').encode('utf-8')
    => "‘"
    

    Windows-1251 和 Latin-1 (AKA ISO 8859-1) 不一样,请尝试使用windows-1251 作为编码:

    IO.read('file', :encoding => 'windows-1251')
    

    这会给你一个知道它是 Windows-1251 的字符串。如果你想要 UTF-8,那么也许你想指定 :internal_encoding and :external_encoding:

    IO.read('file', :external_encoding => 'windows-1251', :internal_encoding => 'utf-8')
    

    【讨论】:

    • @Matt:如果您先s = IO.read(..., :encoding => 'windows-1251'),然后查看s.encoding,您会发现它是windows-1251。查看我的更新以获取 UTF-8。
    • 即使数据无效,它也可以使用任何编码。
    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多