Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我认为Ruby UTF-16编码_Ruby_Windows_Encoding_Popen3_Utf 16le - Fatal编程技术网

我认为Ruby UTF-16编码

我认为Ruby UTF-16编码,ruby,windows,encoding,popen3,utf-16le,Ruby,Windows,Encoding,Popen3,Utf 16le,我有一个在Windows上运行的Ruby程序,它使用Open3调用shell命令(已知输出UTF-16): attrs={} attrs[:stdout], attrs[:stderr], status = Open3.capture3(command) unless attrs[:stderr].nil? begin attrs[:stderr].force_encoding(Encoding::UTF_16LE).encode!(Encoding::UTF_8) rescu

我有一个在Windows上运行的Ruby程序,它使用Open3调用shell命令(已知输出UTF-16):

attrs={}
attrs[:stdout], attrs[:stderr], status = Open3.capture3(command)

unless attrs[:stderr].nil?
  begin
    attrs[:stderr].force_encoding(Encoding::UTF_16LE).encode!(Encoding::UTF_8)
  rescue => e
    attrs[:stderr] = attrs[:stderr].bytes.to_json.encode!(Encoding::UTF_8)
  end
end
如果对UTF_16LE的强制编码不起作用,并引发异常,我只需保存字节,将其编码为JSON字符串,并将其编码为UTF_8

嗯……抛出了异常,我在rescue子句中捕获了字节的输出数组。看起来是这样的:

[10,84,104,105,115,32,97,112,112,108,105,99,97,116,105,111,110,32,104,97,115,32,114,101,113,117,101,115,116,101,100,32,116,104,101,32,82,117,110,116,105,109,101,32,116,111,32,116,101,114,109,105,110,97,116,101,32,105,116,32,105,110,32,97,110,32,117,110,117,115,117,97,108,32,119,97,121,46,10,80,108,101,97,115,101,32,99,111,110,116,97,99,116,32,116,104,101,32,97,112,112,108,105,99,97,116,105,111,110,39,115,32,115,117,112,112,111,114,116,32,116,101,97,109,32,102,111,114,32,109,111,114,101,32,105,110,102,111,114,109,97,116,105,111,110,46,10]
"कुत्रा".bytes.map{|c| c.chr}.join("").force_encoding('UTF-8') #=> "कुत्रा"
如何将其转换回某种格式的文本。e、 g.如果我这样做:

irb> "dog".bytes
=> [100, 111, 103]
irb> "कुत्रा".bytes
=> [224, 164, 149, 224, 165, 129, 224, 164, 164, 224, 165, 141, 224, 164, 176, 224, 164, 190]
是否有一种方法可以通过编程将[100111103]转换为“狗”或[224164149224165 129 224164164164224165]转换回“狗”कुत्रा" ? 有没有办法弄清楚我的字节输出数组是什么意思

-------------------------更新---------------------------

我仔细研究了一下,但花了一些时间,因为“解码”不是一件事。但是,我用变量消息中的数组执行了以下操作:

message.map{|c| c.chr}.join("")

=> "\nThis application has requested the Runtime to terminate it in an unusual way.\nPlease contact the application's support team for more information.\n" 
所以我的问题解决了,因为错误消息不在UTF-16LE中

然而,当我这样做时,我得到了如下结果:

irb> "कुत्रा".bytes.map{|c| c.chr}.join("")

=> "\xE0\xA4\x95\xE0\xA5\x81\xE0\xA4\xA4\xE0\xA5\x8D\xE0\xA4\xB0\xE0\xA4\xBE" 

如何将这个外观奇怪的字符串或字节序列转换为更有意义的“कुत्रा“?

在回答有关字节的第一个问题时,请查看数组中的Pack方法:

“U*”格式化尝试在字节数组中匹配尽可能多的UTF8字符

如果在错误消息中使用该方法,则会得到:

"\nThis application has requested the Runtime to terminate it in an unusual way.\nPlease contact the application's support team for more information.\n"
-------------------------更新---------------------------

刚刚注意到你把第一部分弄明白了,并添加了一个新问题

如何将这个奇怪的字符串或字节序列转换为 更有意义”कुत्रा“

执行
“string”.bytes.map{| c | c.chr}.join(“”)
时,新字符串上的字节相同,但编码丢失。可以在此处看到:

s = "dog"
s.encoding #=> #<Encoding:UTF-8>
s = "dog".bytes.map{|c| c.chr}.join("") #=> "dog"
s.encoding #=> #<Encoding:US-ASCII>

希望它有助于回答您关于字节的第一个问题,请查看数组中的Pack方法:

“U*”格式化尝试在字节数组中匹配尽可能多的UTF8字符

如果在错误消息中使用该方法,则会得到:

"\nThis application has requested the Runtime to terminate it in an unusual way.\nPlease contact the application's support team for more information.\n"
-------------------------更新---------------------------

刚刚注意到你把第一部分弄明白了,并添加了一个新问题

如何将这个奇怪的字符串或字节序列转换为 更有意义”कुत्रा“

执行
“string”.bytes.map{| c | c.chr}.join(“”)
时,新字符串上的字节相同,但编码丢失。可以在此处看到:

s = "dog"
s.encoding #=> #<Encoding:UTF-8>
s = "dog".bytes.map{|c| c.chr}.join("") #=> "dog"
s.encoding #=> #<Encoding:US-ASCII>
希望能有帮助

有没有办法通过编程将[100111103]转换为“dog”

对于其他字母,请尝试相同或“कुत्रा“.bytes.pack('U*')。 我不能在我的电脑里使用那些马拉地语(它也意味着“狗”lol)

如何将这个外观奇怪的字符串或字节序列转换为更有意义的“कुत्रा“

基本上是:

puts "\xE0\xA4\x95\xE0\xA5\x81\xE0\xA4\xA4\xE0\xA5\x8D\xE0\xA4\xB0\xE0\xA4\xBE"
有没有办法通过编程将[100111103]转换为“dog”

对于其他字母,请尝试相同或“कुत्रा“.bytes.pack('U*')。 我不能在我的电脑里使用那些马拉地语(它也意味着“狗”lol)

如何将这个外观奇怪的字符串或字节序列转换为更有意义的“कुत्रा“

基本上是:

puts "\xE0\xA4\x95\xE0\xA5\x81\xE0\xA4\xA4\xE0\xA5\x8D\xE0\xA4\xB0\xE0\xA4\xBE"

值得一提的是--
U
表示UTF-8,而不是UTF-16/UCS2。如果您实际处理的是UTF-16,那么类似的内容可能是合适的(未经测试):
bytes.map(&:chr).join.force\U编码(encoding::UTF\U 16)
。值得一提的是--
U
表示UTF-8,而不是UTF-16/UCS2。如果您实际上是在处理UTF-16,类似的内容可能是合适的(未经测试):
bytes.map(&:chr.join.force\U encoding(encoding::UTF\U 16)
。我回答了您更新的问题了吗?我回答了您更新的问题了吗?