# Writing accented characters into HTML files?

**URL:** <https://www.ruby-forum.com/t/writing-accented-characters-into-html-files/156958>\
**Category:** Ruby\
**Created:** [January 5, 2009, 8:59pm UTC](https://www.ruby-forum.com/t/writing-accented-characters-into-html-files/156958 "2009-01-05T20:59:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [January 5, 2009, 8:59pm UTC](https://www.ruby-forum.com/t/writing-accented-characters-into-html-files/156958/1 "2009-01-05T20:59:10Z")

</div>

I’m having trouble when I write accented characters into HTML files;  
though the accents appear properly in my terminal, they are badly  
“messed up” in the HTML output. CGI.escape doesn’t fix the problem,  
because these are not “special” characters line \< or \>, but simply  
accented e’s, o’s, etc. I’m assuming the problem has something to do  
with a character set type mismatch between the file Ruby is writing  
and what the browser (Firefox) expects, but I’m at a loss as to how to  
correct it.

Any advice most appreciated,  
Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [January 6, 2009, 12:05am UTC](https://www.ruby-forum.com/t/writing-accented-characters-into-html-files/156958/2 "2009-01-06T00:05:52Z")

</div>

Look into using a reference. Valid references are dependent on the  
version of the HTML used.

> **[Character encodings in HTML](https://en.wikipedia.org/wiki/Character_encodings_in_HTML)**
>
> While Hypertext Markup Language (HTML) has been in use since 1991, HTML 4.0 from December 1997 was the first standardized version where international characters were given reasonably complete treatment. When an HTML document includes special characters outside the range of seven-bit ASCII, two goals are worth considering: the information's integrity, and universal browser display.
> There are two general ways to specify which character encoding is used in the document.
> First, the web server can...

> **[List of XML and HTML character entity references](https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references)**
>
> In SGML, HTML and XML documents, the logical constructs known as character data and attribute values consist of sequences of characters, in which each character can manifest directly (representing itself), or can be represented by a series of characters called a character reference, of which there are two types: a numeric character reference and a character entity reference. This article lists the character entity references that are valid in HTML and XML documents.
> A character entity referenc...

Gerald

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [January 6, 2009, 10:19am UTC](https://www.ruby-forum.com/t/writing-accented-characters-into-html-files/156958/3 "2009-01-06T10:19:52Z")

</div>

Kenneth McDonald wrote:

> Any advice most appreciated,

Use hexdump -C on the file to see what the actual byte sequences are. If  
these are single-byte characters then it’s probably ISO-8859-1. If they  
are two bytes then it’s probably UTF-8.

You can use an XML declaration and/or a tag in the section  
to tell the browser which character set your document is in, and/or get  
your web server to set the correct charset in the Content-Type header.

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [January 6, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/writing-accented-characters-into-html-files/156958/4 "2009-01-06T17:28:40Z")

</div>

On Jan 6, 2009, at 3:20 AM, Brian C. wrote:

> Kenneth McDonald wrote:
> 
> > Any advice most appreciated,
> 
> Use hexdump -C on the file to see what the actual byte sequences  
> are. If  
> these are single-byte characters then it’s probably ISO-8859-1. If  
> they  
> are two bytes then it’s probably UTF-8.

I have some code that detects valid UTF-8 data here:

[http://blog.grayproductions.net/articles/the\_unicode\_character\_set\_and\_encodings#comment\_14649](http://blog.grayproductions.net/articles/the_unicode_character_set_and_encodings#comment_14649)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [January 6, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/writing-accented-characters-into-html-files/156958/5 "2009-01-06T14:39:27Z")

</div>

Kenneth McDonald said…

> I’m having trouble when I write accented characters into HTML files;  
> though the accents appear properly in my terminal, they are badly  
> “messed up” in the HTML output. CGI.escape doesn’t fix the problem,  
> because these are not “special” characters line \< or \>, but simply  
> accented e’s, o’s, etc. I’m assuming the problem has something to do  
> with a character set type mismatch between the file Ruby is writing  
> and what the browser (Firefox) expects, but I’m at a loss as to how to  
> correct it.
> 
> Any advice most appreciated,

Start by ensuring that you have the following at the top of

Also, post the “messed up” characters; they’ll tell us something about  
the encoding problem.

Oh, and make sure your editor is writing utf-8.
