Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a character code back to a character?

Tags:

string

ruby

How to I get the Fixnum returned by the following:

"abc"[2] 

Back into a character?

like image 894
Peter Coulton Avatar asked Oct 05 '08 00:10

Peter Coulton


People also ask

How do you change ASCII to character?

To convert ASCII code to a character, you can use different methods such as Type Casting, toString() method, and toChars() method of the Character class. The toString() method will return a character as a String, while the toChars() method returns the array of characters.

How do I convert ASCII to character in Excel?

The Microsoft Excel CHAR function returns the character based on the ASCII value. The CHAR function is a built-in function in Excel that is categorized as a String/Text Function. It can be used as a worksheet function (WS) in Excel.

Which function returns the character equivalent of an ASCII code?

The Char function converts a decimal ASCII code into its associated character. This function supports returning any of the standard 128 characters assigned an ASCII code.

How do I return ASCII value in Python?

In Python 3, the ord () function works to obtain the ASCII value of a character. The function requires a character to be passed as its parameter. It will return the corresponding ASCII value.


2 Answers

This will do it (if n is an integer):

n.chr
like image 139
Greg Hewgill Avatar answered Oct 02 '22 20:10

Greg Hewgill


Be careful because Ruby 1.9 and later will return a single-character string for "abc"[2], which will not respond to the chr method. You could do this instead:

"abc"[2,1]

Be sure to read up on the powerful and multifaceted String#[] method.

like image 20
Grant Hutchins Avatar answered Oct 02 '22 21:10

Grant Hutchins