Text encodings

X

xTRiM

Hello,

is there any way, to detect text encoding?
For example, is it in utf8, or in win1251, or something else.

Thank you.
 
P

Paul Battley

is there any way, to detect text encoding?
For example, is it in utf8, or in win1251, or something else.

You can't detect one-byte-per-character encodings easily (i.e. without
statistical analysis) but you can easily tell if something's UTF-8 or
not:

class String
def is_utf8?
unpack('U*')
return true
rescue
return false
end
end

"foo".is_utf8? #=> true
"foo\303".is_utf8? #=> false

Not the most efficient way, necessarily, but probably the easiest.

Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top