ASCII => char
97.chr #=> "a"
char => ASCII
'a'.ord #=> 97 '\t'.ord #=> 92
字符串可以试试 pack/unpack
[104, 101, 108, 108, 111].pack 'c*' 'hello'.unpack 'c*'
有个简单通用的方法:
"中文abc".codepoints => [20013, 25991, 97, 98, 99]
倒过来:
"中文abc".codepoints.map{|x| x.chr('UTF-8') }.join => "中文abc"
#2 楼 @gihnius 这个好!!
#1 楼 @Shy07 👌