# Accent stress - ASCII - How to get rid of strange characters

**URL:** <https://www.ruby-forum.com/t/accent-stress-ascii-how-to-get-rid-of-strange-characters/75013>\
**Category:** Rails\
**Created:** [November 7, 2006, 11:06am UTC](https://www.ruby-forum.com/t/accent-stress-ascii-how-to-get-rid-of-strange-characters/75013 "2006-11-07T11:06:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2006, 11:06am UTC](https://www.ruby-forum.com/t/accent-stress-ascii-how-to-get-rid-of-strange-characters/75013/1 "2006-11-07T11:06:24Z")

</div>

I just spent some hours to solve a problem that many people must have :  
How to correctly show Latin accent characters in rails?

The problem is that usually, you get all ‘?’ when sending to the  
browser things like ‘ç’, ‘ã’ etc.

I spent many hours dealing with this, but couldn’t find a good solution  
from Mr. Google.

So, I wrote the following method :

def to\_h(string)  
saida=""  
asci = Hash.new  
for i in 1…255  
asci[i.chr] = i  
end  
string.each\_char do |charac|  
saida = saida+ “&#”+asci[charac].to\_s  
end  
return saida  
end

You should put it either in the application\_helper.rb and at the  
controllers application.rb, so that you can call it either from a view  
and from a controller.

Basically, it encodes the string in ‘&#’ codes that are standard html  
for the ‘monkey’ browser.

Sweet !  
Now :

\<%=to\_h(“atenção”) %\>

outputs as :

atenção

and not as aten??o 🙂

Sorry that it is not elegant enough, I had to make a ascii hash, since  
I couldn’t find a standard Ruby function to do this.

If someone can help, please post.

Regards,

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2006, 11:06am UTC](https://www.ruby-forum.com/t/accent-stress-ascii-how-to-get-rid-of-strange-characters/75013/2 "2006-11-07T11:06:25Z")

</div>

OK.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2006, 11:06am UTC](https://www.ruby-forum.com/t/accent-stress-ascii-how-to-get-rid-of-strange-characters/75013/3 "2006-11-07T11:06:32Z")

</div>

Hi, just knew that the [0] can return the ascii code :  
e.g. : “A”[0] \>\> 65  
So, the method is :

def to\_h(string)  
saida=""  
string.each\_char do |charac|  
saida = saida+ “&#”+charac[0].to\_s  
end  
return saida  
end
