I'm looking for something like Python's ord(char)
for Julia that returns an integer.
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). ForChar
, this is aUInt32
value, butAbstractChar
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With