STDIN和Powershell - 如何使编码匹配?

SY.*_*SY. 5 ruby powershell encoding utf-8 cp1250

我有Ruby(1.9.3)和Powershell的问题.

我需要编写一个交互式控制台应用程序,它将处理波兰语的句子.我得到了帮助,可以使用波兰语变音符号检索ARGV元素,但标准输入不能按照我的要求运行.

代码图:

# encoding: UTF-8
target = ARGV[0].dup.force_encoding('CP1250').encode('UTF-8')
puts "string constant = dup?"
puts "dup?".bytes.to_a.to_s
puts "dup?".encoding

puts "target = " +target
puts target.bytes.to_a.to_s
puts target.encoding
puts target.eql? "dup?"

STDIN.set_encoding("CP1250", "UTF-8") 
# the line above changes nothing, it can be removed and the result is still the same
# I obviously wanted to mimic the ARGV solution

target2 = STDIN.gets
puts "target2 = " +target2
puts target2.bytes.to_a.to_s
puts target2.encoding
puts target2.eql? "dup?"
Run Code Online (Sandbox Code Playgroud)

输出:

string constant = dup?
[100, 117, 112, 196, 133]
UTF-8
target = dup?
[100, 117, 112, 196, 133]
UTF-8
true
dup? //this is fed to STDIN.gets
target2 = dup
[100, 117, 112]
UTF-8
false
Run Code Online (Sandbox Code Playgroud)

显然Ruby永远不会从STDIN.gets获得第四个字符.如果我写一个更长的字符串,dup?lalala仍然只有三个初始字节出现在程序中.

  • 我已经尝试枚举字节并使用getc循环,但它们似乎永远不会到达Ruby(它们丢失在哪里?)
  • 我用过chcp 65001(似乎没有改变一件事)
  • 我已将$ OutputEncoding更改为[Console] :: OutputEncoding; 它现在看起来像这样:

     IsSingleByte      : True
     BodyName          : ibm852
     EncodingName      : ?rodkowoeuropejski (DOS)
     HeaderName        : ibm852 
     WebName           : ibm852
     WindowsCodePage   : 1250
     IsBrowserDisplay  : True
     IsBrowserSave     : True
     IsMailNewsDisplay : False
     IsMailNewsSave    : False
     EncoderFallback   : System.Text.InternalEncoderBestFitFallback
     DecoderFallback   : System.Text.InternalDecoderBestFitFallback
     IsReadOnly        : True
     CodePage          : 852
    
    Run Code Online (Sandbox Code Playgroud)
  • 我正在使用Consolas字体

如何在Powershell中正确阅读波兰语变音符号?

Χpẘ*_*Χpẘ 1

我查到了一些相关的信息。不确定这是否是正确的信息。但是,嘿,OP 已经有了另一个解决方案。

\n\n
# Get "encoding" for code page 1250 (Central European)\n$en=[System.Text.Encoding]::GetEncoding(1250)\n# Looks like this:\nIsSingleByte      : True\nBodyName          : iso-8859-2\nEncodingName      : Central European (Windows)\nHeaderName        : windows-1250\nWebName           : windows-1250\nWindowsCodePage   : 1250\nIsBrowserDisplay  : True\nIsBrowserSave     : True\nIsMailNewsDisplay : True\nIsMailNewsSave    : True\nEncoderFallback   : System.Text.InternalEncoderBestFitFallback\nDecoderFallback   : System.Text.InternalDecoderBestFitFallback\nIsReadOnly        : True\nCodePage          : 1250\n\n# Change STDIN\'s input encoding\n[console]::InputEncoding=$en\n$x = Read-Host \n# I typed in dup\xc4\x85 \n#  (I set Polish in Languate Bar. \n#   Final letter is apostrophe on US English keyboard)\n[int[]][char[]]$x\n# output is: 100 117 112 261 (in hex): 64 75 70 105\n# the final character (261) is "Latin Small Letter A with Ogonek" \n
Run Code Online (Sandbox Code Playgroud)\n