如何在Julia中找到字符的unicode值?

问题描述 投票:6回答:2

我正在寻找类似Julia的Python ord(char)之类的东西,它返回一个整数。

julia
2个回答
6
投票

我认为您正在寻找codepoint。从文档中:

codepoint

返回与字符codepoint(c::AbstractChar) -> Integer对应的Unicode代码点(无符号整数)(如果c不代表有效字符,则抛出异常)。对于c,这是一个Char值,但是仅代表Unicode子集的UInt32类型可能会返回不同大小的整数(例如AbstractChar)。

例如:

UInt8

要获得与Python的julia> codepoint('a') 0x00000061 函数完全等效的结果,您可能希望将结果转换为有符号整数:

ord

0
投票

您也可以这样做:

julia> Int(codepoint('a'))
97

如果您有字符串:

julia> Int('a')
97

更多详细信息julia> s="hello"; julia> Int(s[1]) 104 julia> Int(s[2]) 101 julia> Int(s[5]) 111

© www.soinside.com 2019 - 2024. All rights reserved.