Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the unicode value of a character in Julia?

Tags:

julia

I'm looking for something like Python's ord(char) for Julia that returns an integer.

like image 636
vboreda514 Avatar asked Mar 14 '20 21:03

vboreda514


2 Answers

I think you're looking for codepoint. From the documentation:

codepoint(c::AbstractChar) -> Integer

Return the Unicode codepoint (an unsigned integer) corresponding to the character c (or throw an exception if c does not represent a valid character). For Char, this is a UInt32 value, but AbstractChar types that represent only a subset of Unicode may return a different-sized integer (e.g. UInt8).

For example:

julia> codepoint('a')
0x00000061

To get the exact equivalent of Python's ord function, you might want to convert the result to a signed integer:

julia> Int(codepoint('a'))
97
like image 105
François Févotte Avatar answered Nov 01 '22 05:11

François Févotte


You can also just do:

julia> Int('a')
97

If you have a String:

julia> s="hello";

julia> Int(s[1])
104

julia> Int(s[2])
101

julia> Int(s[5])
111

More details here.

like image 37
4lrdyD Avatar answered Nov 01 '22 05:11

4lrdyD