【问题标题】:LUA : How to print a Latin1 string with io.write()?LUA:如何使用 io.write() 打印 Latin1 字符串?
【发布时间】:2021-07-25 03:29:06
【问题描述】:

在 Lua 5.4 中,我尝试使用 io.write() 以 Latin1 编码打印单字符串,但某些字符 (à,é...) 打印效果不佳,

我该怎么做?

这是使用 win-125x.lua 打印失败的截图

【问题讨论】:

  • 这取决于终端中使用的编码。 Lua 与字符串中的编码无关。
  • io.write(win_to_utf8(your_string)),使用 this 模块,为 Latin1 设置 codepage = 1252
  • 我试过了,还是不行:/
  • 请张贴€€€ 输入的“打印不好”输出的屏幕截图。
  • 它打印 ???而不是€€€

标签: windows lua


【解决方案1】:

我猜你是在 Windows 上运行 Lua。
因为您要将 Latin1 字符转换为 UTF8,所以您应该在运行 Lua 脚本之前将 Windows 控制台代码页设置为 UTF8,使用以下命令:

chcp 65001

另一种选择是使用 UTF8 编码保存脚本,而无需将字符串从 cp1252 转换为 UTF8,并在运行脚本之前使用 chcp 命令。

请记住,标准 Lua 没有字符串编码的概念,并且 Windows 对控制台中的 UTF8 字符的支持是不完整的。因此出现了这种问题。

也检查相关问题:Problem with accents while copying files in LUA

【讨论】:

  • chcp 1252 用于通常的非 UTF8 编码
【解决方案2】:

如果你有桌子utf8,你可以这样做......

> do io.write(utf8.char(8364):rep(3)..'\n'):flush() end
€€€

要获取你可以做的代码...

> do io.write(utf8.codepoint('€')..'\n'):flush() end
8364

但我不确定这是否适用于 Windows。
...我在 linux 上。

【讨论】:

  • 由于windows上的代码页错误,它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-03-09
  • 2015-12-31
相关资源
最近更新 更多