What's the opposite of chr() in Ruby?

前端 未结 10 938
自闭症患者
自闭症患者 2020-12-23 12:53

In many languages there\'s a pair of functions, chr() and ord(), which convert between numbers and character values. In some languages, ord()

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-23 13:42

    Just came across this while putting together a pure Ruby version of Stringprep via RFCs.

    Beware that chr fails outside [0,255], instead use 1.9.x - 2.1.x portable replacements:

    [22] pry(main)> "\u0221".ord.chr
    RangeError: 545 out of char range
    from (pry):2:in 'chr'
    [23] pry(main)> x = "\u0221".unpack('U')[0]
    => 545
    [24] pry(main)> [x].pack('U')
    => "ȡ"
    [25] pry(main)>
    

提交回复
热议问题