Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the encoding of the string in Ruby

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.

like image 270
FrozenHeart Avatar asked Aug 14 '14 08:08

FrozenHeart


People also ask

What encoding is string in C#?

All strings in a . NET Framework program are stored as 16-bit Unicode characters.

What is the sequence of Ruby string in bytes?

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.


1 Answers

  • How can I determine the encoding of this string?

Use this method #encoding- Returns the Encoding object that represents the encoding of obj.

  • Then I need to convert it to the Windows console encoding so I can print it...

Take look at the class Encoding to get the ideas, about how to convert to required encodings.

like image 96
Arup Rakshit Avatar answered Nov 15 '22 07:11

Arup Rakshit