【问题标题】:Ruby on Windows displaying wrong unicode character in cmd terminalWindows 上的 Ruby 在 cmd 终端中显示错误的 unicode 字符
【发布时间】:2019-12-06 03:18:20
【问题描述】:

标题几乎概括了它。 我正在尝试在 win10 命令提示符中显示国际象棋 unicode 字符,而是显示替换字符 (�)(复制粘贴再次给我正确的字符)

强制执行 UTF-8 编码。 将 CMD Charpage 设置为 UTF-8 编码。 在我的字符串中使用 unicode 字符而不是代码。

$icons = {
  "Pawn" => "\u2659", # ♙
  "Rook" => "\u2656", # ♖
  "Knight" => "\u2658", # ♘
  "Bishop" => "\u2657", # ♗
  "Queen" => "\u2655", # ♕
  "King" => "\u2654", # ♔

  "Black" => "\u25A0", # ■
  "White" => " " #
}

class Graphics
  def display_board(b);
  system ("cls")
  #system ("chcp 65001")
  board = b.get_map();
    board.each_with_index do |subarr,x|
      str = "";

      subarr.each_with_index do |value,y|
        str += "[" + $icons[value] + "]";
      end

      puts(str);
    end
  end
end

预期结果是这样的:

[♖][♘][♗][♔][♕][♗][♘][♖]
[♙][♙][♙][♙][♙][♙][♙][♙]
[ ][■][ ][■][ ][■][ ][■]
[■][ ][■][ ][■][ ][■][ ]
[ ][■][ ][■][ ][■][ ][■]
[■][ ][■][ ][■][ ][■][ ]
[♙][♙][♙][♙][♙][♙][♙][♙]
[♖][♘][♗][♔][♕][♗][♘][♖]

实际结果: � 对于每一个棋子。

【问题讨论】:

  • 您的终端使用什么代码集?终端中的不匹配将导致输出中出现类似的字符。我在为 UTF-8 设置的 Mac OS 终端中得到了合理的输出:=> {"Pawn"=>"♙", "Rook"=>"♖", "Knight"=>"♘", "Bishop"=>"♗", "Queen"=>"♕", "King"=>"♔", "Black"=>"■", "White"=>" "}
  • 查看我的更新答案。您需要在 Windows 上使用支持更广泛的 UTF-8 的字体。

标签: ruby windows shell unicode cmd


【解决方案1】:

这似乎不是 ruby​​ 问题,而是 windows 的默认编码设置以及命令提示符 cmd.exe 使用的字体。

以下是您应该如何使其工作的方法。

  1. 下载并安装this free monospaced font
  2. 右键单击Windows开始按钮并选择Run然后输入regedit并输入
  3. 输入以下路径HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont
  4. 右键单击关键条目下的白色区域并选择new > string value
  5. 在数值数据框中输入Droid Sans Mono
  6. 单击“确定”,然后重新启动 Windows。
  7. 右键单击windows按钮>运行>cmd
  8. 右键单击窗口顶部并选择 DejaVue Sans Mono 字体
  9. 输入ruby -e 'puts "\u2658"' 进行测试

你应该看到一个输出骑士的字体 ♘

如需更多帮助,请参阅https://www.thewindowsclub.com/add-custom-fonts-to-command-prompt

对于其他可能使用的字体,请参阅this answer上的建议

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 2023-03-22
    • 2010-12-20
    • 2021-12-30
    • 2015-07-03
    相关资源
    最近更新 更多