Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding in R like Python ("ord" and "chr")

I was wondering how to do encoding and decoding in R. In Python, we can use ord('a') and chr(97) to transform a letter to number or transform a number to a letter. Do you know any similar functions in R? Thank you!

For example, in python

>>>ord("a")

97

>>>ord("A")

65

>>>chr(97)

'a'

>>>chr(90)

'Z'

FYI: ord(c) in Python Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string. For example, ord('a') returns the integer 97, ord(u'\u2020') returns 8224. This is the inverse of chr() for 8-bit strings and of unichr() for unicode objects. If a unicode argument is given and Python was built with UCS2 Unicode, then the character’s code point must be in the range [0..65535] inclusive; otherwise the string length is two, and a TypeError will be raised.

chr(i) in Python Return a string of one character whose ASCII code is the integer i. For example, chr(97) returns the string 'a'. This is the inverse of ord(). The argument must be in the range [0..255], inclusive; ValueError will be raised if i is outside that range. See also unichr().

like image 546
user2345559 Avatar asked May 03 '13 04:05

user2345559


People also ask

What is ord () and CHR ()?

Python ord() and chr() are built-in functions. They are used to convert a character to an int and vice versa. Python ord() and chr() functions are exactly opposite of each other.

What is CHR ord (' A?

Python chr() and ord() Python's built-in function chr() is used for converting an Integer to a Character, while the function ord() is used to do the reverse, i.e, convert a Character to an Integer.

What is ord () in Python?

The ord() function returns the number representing the unicode code of a specified character.

What does CHR () do in Python?

The chr() function returns the character that represents the specified unicode.


1 Answers

You're looking for utf8ToInt and intToUtf8

utf8ToInt("a")
[1] 97

intToUtf8(97)
[1] "a"
like image 172
Ricardo Saporta Avatar answered Oct 04 '22 14:10

Ricardo Saporta