Alexwall
Alexwall

Reputation: 667

Trouble making a heart symbol in Lua?

I was wondering how to make the heart sign or "♥" in Lua, I have tried \003 because that is the ASCII code for it, but it does not print it out.

Upvotes: 1

Views: 558

Answers (2)

Tom Blodget
Tom Blodget

Reputation: 20812

This has little to do with Lua.

You need to find out which character set and encoding is used in your environment and select a font that supports ♥ in that encoding.

Then you need to use an editor for your Lua script that saves in that encoding. If that part is not possible then you can determine the byte sequence required, code it as numeric escapes in a literal string and save in a compatible encoding such as CP437. For example, if you are outputting to a UTF-8 processor, "\xE2\x99\xA5".

Keep in mind that a Lua string is a counted sequence of bytes. It's up to you and your editor to put the right bytes in in the file, it's up to your environment (e.g., console) to interpret those bytes in a particular character encoding, and up to the font to display the glyph.

In a Windows console, you can select the Lucinda Console font, chcp 65001 to use UTF-8 and use Lua 5.1 like this: lua -e "print('\226\153\165')". As a comparison, chcp 437 to use IBM437 and use Lua 5.1 like this: lua -e "print('\003')".

Upvotes: 3

Yu Hao
Yu Hao

Reputation: 122493

For ASCII, only range 0x20 to 0x7E are printable. Others, including 0x03, isn't printable. Printing its value would be up to the implementation.

If the environment supports Unicode, you can simply call:

print("♥")

For instance, Lua Demo outputs , same in ideone.

Upvotes: 2

Related Questions