# Character encoding question

**URL:** <https://www.ruby-forum.com/t/character-encoding-question/185825>\
**Category:** Ruby\
**Created:** [March 26, 2010, 6:27pm UTC](https://www.ruby-forum.com/t/character-encoding-question/185825 "2010-03-26T18:27:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [March 26, 2010, 6:27pm UTC](https://www.ruby-forum.com/t/character-encoding-question/185825/1 "2010-03-26T18:27:29Z")

</div>

I have an html file which is encoded in UTF-8. The file contains the  
following text:

It's a wonderful life

now the character code 39 is for aphostrohpe in UTF8. so suppose I got  
the 39 out of the text using:

s=“It’s a wonderful life”

s.gsub(/&#(\d+);/, ‘\1’)

The output is

It39s a wonderful life

So firstly I am having trouble making it

It\39s a wonderful life

Secondly I manually did this in test\_utf8.rb:

puts “It\39s a wonderful life”

and ran it

ruby test\_utf8.rb \> utf8.txt

but by opening it in the open office by setting the encoding to utf-8  
the output is

It#9s a wonderful life

So how to correctly parse the collect and convert html character  
reference to encoded charcters in utf-8 and then save file?

Thanks.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [March 26, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/character-encoding-question/185825/2 "2010-03-26T19:17:05Z")

</div>

> s=“It’s a wonderful life”

## I stumbled across this:

require ‘cgi’  
s=CGI.unescapeHTML(“It’s a wonderful life”)

* * *

David

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [March 26, 2010, 9:22pm UTC](https://www.ruby-forum.com/t/character-encoding-question/185825/3 "2010-03-26T21:22:29Z")

</div>

## try something like this:

## require ‘cgi’ s=“UPPERCASE Russian Alphabet\n”.encode(‘utf-8’) s+=CGI.unescapeHTML(“АБВГ”.encode(‘utf-8’)) s+=CGI.unescapeHTML(“ДЕЖЗ”.encode(‘utf-8’)) s+=CGI.unescapeHTML(“ИЙКЛ”.encode(‘utf-8’)) s+=CGI.unescapeHTML(“МНОП”.encode(‘utf-8’)) s+=CGI.unescapeHTML(“РСТУ”.encode(‘utf-8’)) s+=CGI.unescapeHTML(“ФХЦЧ”.encode(‘utf-8’)) s+=CGI.unescapeHTML(“ШЩЪЫ”.encode(‘utf-8’)) s+=CGI.unescapeHTML(“ЬЭЮЯ”.encode(‘utf-8’)) puts s

David
