Search code examples
windowslua

LUA : How to print a Latin1 string with io.write()?


In Lua 5.4, I tried to print sone strings in Latin1 encoding with io.write(), but some characters (à,é...) are not well printed,

How could I perform this ?

Here is a screenshot of failed print with win-125x.lua

[1]: should be €€€


Solution

  • I guess you are running Lua on Windows.
    Because you are converting Latin1 characters to UTF8, you should set the Windows console codepage to UTF8 before running your Lua script, with the following command :

    chcp 65001
    

    An other option is to save your script with UTF8 encoding without the need to convert strings from cp1252 to UTF8 and use the chcp command before running your script.

    Remember that standard Lua has no concept of string encoding and that Windows support for UTF8 characters in the console is incomplete. Hence this kind of problems.

    Check that related question too : Problem with accents while copying files in LUA