3 ms·
I just had to write this code in Ruby to convert a list of integers from an Elixir log file into some text that I could read def to_string(s) s.split(",
by pmontra 7y ago
I just had to write this code in Ruby to convert a list of integers from an Elixir log file into some text that I could read
def to_string(s)
s.split(", ").map { |dec|
dec.to_i.chr(Encoding::UTF_8)
}.join
end
"100, 8217, 65, 118, 111, 114, 105, 111".to_string
=> d’Avorio
I had to specify the encoding because an unassisted Fixnum#chr works only for ASCII values. However I can imagine that it could use some intelligence to figure out the correct encoding. Probably it's not worth it and it could make mistakes.
- lmm 7y agoExactly - "turn this value from this specified encoding into a character" is semantically much clearer than "make a character whose internal representation is this integer".