I have a code to transform from string to binary:
> s = "AnatomÃa Según Grey"
=> "AnatomÃa Según Grey"
> s.unpack("a*").first
=> "Anatom\xC3\x83\xC2\xADa Seg\xC3\x83\xC2\xBAn Grey"
How can I get the original string (undo the unpack
)?
The conversion is equivalent to:
s = "AnatomÃa Según Grey"
#=> "AnatomÃa Según Grey"
s.force_encoding('BINARY')
#=> "Anatom\xC3\x83\xC2\xADa Seg\xC3\x83\xC2\xBAn Grey"
It can be "reversed" via: (you might have to adjust the 'UTF-8'
part)
s.force_encoding('UTF-8')
#=> "AnatomÃa Según Grey"
force_encoding
doesn't change the string, it just defines how the bytes are interpreted.
To fix the string, you probably have to call encode
in addition:
"AnatomÃa Según Grey".encode('ISO-8859-1').force_encoding('UTF-8')
#=> "Anatomía Según Grey"