# Converting escaped html to utf-8

**URL:** <https://www.ruby-forum.com/t/converting-escaped-html-to-utf-8/110401>\
**Category:** Ruby\
**Created:** [July 26, 2007, 1:31am UTC](https://www.ruby-forum.com/t/converting-escaped-html-to-utf-8/110401 "2007-07-26T01:31:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Worrall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@Chris\_Worrall](https://www.ruby-forum.com/u/Chris_Worrall)\
**Post date:** [July 26, 2007, 1:31am UTC](https://www.ruby-forum.com/t/converting-escaped-html-to-utf-8/110401/1 "2007-07-26T01:31:31Z")

</div>

Hi everyone,

I’ve looked around online for a solution, but I’m pretty new to ruby  
and programming in general, so I feel like I’m hitting a wall here.

I’m retrieving data from Hpricot that I’d like to store in UTF-8, but  
I can’t find a function to convert hex NCRs like:

á

Surely somebody’s had to do this in the past that could point me in  
the right direction? Thanks!

---

<div class="post-metadata">

**Author:** ![Chris\_Worrall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@Chris\_Worrall](https://www.ruby-forum.com/u/Chris_Worrall)\
**Post date:** [July 26, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/converting-escaped-html-to-utf-8/110401/2 "2007-07-26T18:41:02Z")

</div>

Well, after some more googling, I found a solution. If anyone was  
curious –

require ‘cgi’  
require ‘iconv’

n = “á”  
n = CGI.unescapeHTML(n)  
n = Iconv.conv(“UTF-8”, “ISO-8859-1”, n)

---

<div class="post-metadata">

**Author:** ![Chris\_Worrall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@Chris\_Worrall](https://www.ruby-forum.com/u/Chris_Worrall)\
**Post date:** [July 27, 2007, 12:55am UTC](https://www.ruby-forum.com/t/converting-escaped-html-to-utf-8/110401/3 "2007-07-27T00:55:45Z")

</div>

Chris Worrall wrote:

> Well, after some more googling, I found a solution. If anyone was  
> curious –
> 
> require ‘cgi’  
> require ‘iconv’
> 
> n = “á”  
> n = CGI.unescapeHTML(n)  
> n = Iconv.conv(“UTF-8”, “ISO-8859-1”, n)

I’m surprised no one mentioned it but you could use

require “rubygems”  
require “htmlentities”  
puts HTMLEntities.decode\_entities(“Ā Ĉ Ď”)  
=\> Ä€ Äˆ ÄŽ

Daniel
