如何解决命令执行中的UTF-8问题

时间:2018-08-17 09:41:01

标签: ruby windows encoding utf-8 windows-console

当我使用反引号执行命令并使用puts打印输出时,它显示问号而不是正确的字符。 这是我的代码(只是为了使您更容易阅读而进行的测试):

# encoding: utf-8

# Trying without encoding after
puts `echo Just ä täst`

# Trying with encoding after
puts `echo Just ä täst`.encode("UTF-8", :invalid=>:replace, :replace=>"0")

输出如下:

Just � t�st
Just 0 t0st

此外,如果我尝试使用

进行拆分
puts `echo Just ä täst`.split(" ") 

我收到以下错误:

./utf_test.rb:9:in `split': invalid byte sequence in UTF-8 (ArgumentError)
    from ./utf_test.rb:9:in `<main>'

但是,当我仅打印到屏幕上时,即可以工作,即puts 'just ä täst'导致just ä täst被打印。

0 个答案:

没有答案
相关问题