I have a string received via the gets.chomp
of the TcpSocket
class. How can I determine the encoding of this string? Then I need to convert it to the Windows console encoding so I can print it. Is there any built-in way or third-party library to do it in Ruby?
Thanks in advance.
All strings in a . NET Framework program are stored as 16-bit Unicode characters.
But the most important evolution is that in Ruby 1.8, strings are considered as a sequence of bytes when in Ruby 1.9, strings are considered as a sequence of codepoints. A sequence of codepoints, coupled to a specific encoding, allows Ruby to handle encodings. Indeed, on disk, a string is stored as a sequence of bytes.
Use this method #encoding
- Returns the Encoding object that represents the encoding of obj.
Take look at the class Encoding
to get the ideas, about how to convert to required encodings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With