Domanda

How encode sequence of bytes from ruby string into ruby string human-readable characters?

This is input string:

"\x127\x00\x06\x00\x00\x00\x01\x00\xA2\x8F"

So how parse this string into array with bytes,

and encode every element from array to ASCII character?

P.S. However, I can't find a way to roundtrip from bytes back to an array. I tried to use Array.pack with the U* option, but that doesn't work for multibyte characters.

È stato utile?

Soluzione

You can try something like:

"string\xaa".each_byte.map {|b| "%c(%x)" % [ b, b ] }.join( ' ' )
# => "s(73) t(74) r(72) i(69) n(6e) g(67) ª(aa)"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top