tags:

views:

145

answers:

3
+1  Q: 

Detect encoding

I'm getting some string data from the web, and I suspect that it's not always what it says it is. I don't know where the problem is, and I just don't care anymore. From day one on this project I've been fighting Ruby string encoding. I really want some way to say: "Here's a string. What the hell is it?", and then use that data to get it to UTF-8 so that it doesn't explode gsub() 2,000 lines down in the depths of my app. I've checked out rchardet, but even though it supposedly works for 1.9 now, it just blows up given any input with multiple bytes... which is not helpful.

A: 

Try setting these in your environment. export LC_ALL=en_US.UTF-8 export LC_CTYPE=en_US.UTF-8

Try ruby -EBINARY ...orruby -EASCII-8BIT to command line

Try adding -Ku or -Kn to your ruby command line.

Could you paste the error message ?

Also try this: http://github.com/candlerb/string19/blob/master/string19.rb

rahul
+1  A: 

It is impossible to tell from a string what encoding it is in. You always need some additional metadata that tells you what the string's encoding is.

If you get the string from the web, that metadata is in the HTTP headers. If the HTTP headers are wrong, there is absolutely nothing that you or Ruby or anyone else can do. You need to file a bug with the webmaster of the site where you got the string from and wait till he fixes it. If you have a Service Level Agreement with the website, file a bug, wait a week, then sue them.

Jörg W Mittag