Display extended-ASCII character in Ruby

How to print an extended ASCII character on the console. For example, if I use the following

puts 57.chr

It will display "9" on the console. If i used

puts 219.chr

It will only display "?". It does this for all Extended-ASCII codes from 128 to 254. Is there a way to display the correct character instead of "?".

+4
source share
4 answers

I am trying to use drawing symbols to create graphics in my console program.

You should now use UTF-8.

Here is an example of using characters in a Box Drawing Unicode block :

puts "╔═══════╗\nβ•‘ Hello β•‘\nβ•šβ•β•β•β•β•β•β•β•"

Conclusion:

╔═══════╗
β•‘ Hello β•‘
β•šβ•β•β•β•β•β•β•β•

, , , Unicode U + 2588 ( ), Ruby.

:

puts "\u2588"

puts 0x2588.chr('UTF-8')

:

puts 'β–ˆ'
+8

, :

219.chr(Encoding::UTF_8)
+4

You need to specify the encoding of the string and then convert it to UTF-8. For example, if I want to use Windows-1251 encoding:

219.chr.force_encoding('windows-1251').encode('utf-8')
# => ""

Similarly for ISO_8859_1:

219.chr.force_encoding("iso-8859-1").encode('utf-8')
# => "Γ›" 
+3
source

You can use the method Integer#chr([encoding]):

219.chr(Encoding::UTF_8)   #=> "Γ›"

more information in the doc method .

0
source

All Articles