Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Extended-ASCII character in Ruby

How can I print an Extended-ASCII character to the console. For instance if I use the following

puts 57.chr

It will print "9" to the console. If I were to use

puts 219.chr

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

like image 993
Michael Avatar asked Feb 24 '15 12:02

Michael


People also ask

How do I display extended ASCII characters?

On a standard 101 keyboard, special extended ASCII characters such as é or ß can be typed by holding the ALT key and typing the corresponding 4 digit ASCII code. For example é is typed by holding the ALT key and typing 0233 on the keypad.

What is the extended ASCII character set?

A set of codes that extends the basic ASCII set. The basic ASCII set uses 7 bits for each character, giving it a total of 128 unique symbols. The extended ASCII character set uses 8 bits, which gives it an additional 128 characters.

Is extended ASCII Unicode?

Some people call any non-ASCII character in Unicode "extended ASCII". In other contexts only the UTF-8 encoding counts, and in yet other contexts no Unicode encoding is considered extended ASCII.

What is ASCII in Ruby?

ASCII stands for “American Standard Code for Information Interchange”. You can find an ASCII table, or you can ask Ruby to convert characters to their ASCII value. If you have an integer you can get the associated character. As we’ll see later in this article, this range of characters is limited.

What are the extended ASCII characters used for?

There are extended ASCII characters that are particularly useful when trying to make a textual user-interface (TUI). For example, when trying to make boxes and separators, like with the ┤ character (ASCII 180).

How do I get the ASCII value of a character?

Starting with ASCII. ASCII stands for “American Standard Code for Information Interchange”. You can find an ASCII table, or you can ask Ruby to convert characters to their ASCII value. If you have an integer you can get the associated character. As we’ll see later in this article, this range of characters is limited. Why?

What is ascii_8bit encoding?

Encoding::ASCII_8BIT is a special encoding that is usually used for a byte string, not a character string. But as the name insists, its characters in the range of ASCII are considered as ASCII characters.


2 Answers

I am trying to using the drawing characters to create graphics in my console program.

You should use UTF-8 nowadays.

Here's an example using characters from the Box Drawing Unicode block:

puts "╔═══════╗\n║ Hello ║\n╚═══════╝"

Output:

╔═══════╗
║ Hello ║
╚═══════╝

So if for instance I was going to use the Unicode character U+2588 (the full block) how would I print that to the console in Ruby.

You can use:

puts "\u2588"

or:

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

or simply:

puts '█'
like image 116
Stefan Avatar answered Sep 30 '22 17:09

Stefan


You may need to specify the encoding, e.g.:

219.chr(Encoding::UTF_8)
like image 32
jam Avatar answered Sep 30 '22 16:09

jam