# Convert ascii codes in a string to a string of characters

**URL:** <https://rubytalk.org/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/48927>\
**Category:** ruby-talk\
**Created:** [8 September 2008 02:13 UTC](https://rubytalk.org/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/48927 "2008-09-08T02:13:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smitty1](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@smitty1](https://rubytalk.org/u/smitty1)\
**Post date:** [8 September 2008 02:13 UTC](https://rubytalk.org/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/48927/1 "2008-09-08T02:13:51Z")

</div>

Is there a more efficient way to convert a string such as  
"&#97;&#98;&#99;&#100;&#101;&#102;" to "abcdef" ?

Currently I am doing the following:  
a="&#97;&#98;&#99;&#100;&#101;&#102;"  
a.delete!("&#")  
ar = a.split(';')  
arn = ar.collect { |s| Integer(s) }  
s = arn.pack("U\*")  
puts "s=#{s.inspect}"

I need to do this for thousands of records that are  
loaded into a database, so I'm a bit concerned  
about repeatedly executing all of the above code.  
Thanks

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [8 September 2008 02:54 UTC](https://rubytalk.org/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/48927/2 "2008-09-08T02:54:19Z")

</div>

# Is there a more efficient way to convert a string such as  
# "&#97;&#98;&#99;&#100;&#101;&#102;" to "abcdef" ?

> **···**
>
> From: smitty [mailto:cleesmith2006@gmail.com]  
> #  
> # Currently I am doing the following:  
> # a="&#97;&#98;&#99;&#100;&#101;&#102;"  
> # a.delete!("&#")  
> # ar = a.split(';')  
> # arn = ar.collect { |s| Integer(s) }  
> # s = arn.pack("U\*")  
> # puts "s=#{s.inspect}"
> 
> i'm feeling good today 😉
> 
> > "&#97;&#98;&#99;&#100;&#101;&#102;".delete("&#").split(";").map{|x| x.to\_i}.pack("U\*")
> 
> =\> "abcdef"
> 
> or
> 
> > "&#97;&#98;&#99;&#100;&#101;&#102;".gsub(/\&\#(\d+);/){|x| $1.to\_i.chr}
> 
> =\> "abcdef"
> 
> > require 'cgi'
> 
> =\> false
> 
> > CGI.unescapeHTML "&#97;&#98;&#99;&#100;&#101;&#102;"
> 
> =\> "abcdef"
> 
> kind regards -botp

---

<div class="post-metadata">

**Author:** ![Chris\_Dew](https://avatars.discourse-cdn.com/v4/letter/c/b38774/32.png) [@Chris\_Dew](https://rubytalk.org/u/Chris_Dew)\
**Post date:** [8 September 2008 13:13 UTC](https://rubytalk.org/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/48927/3 "2008-09-08T13:13:48Z")

</div>

Have you looked at [rubyinline | software projects | by ryan davis](http://www.zenspider.com/ZSS/Products/RubyInline/) ?

You could write the conversion routine in C, which is efficient for  
customised byte manipulation.

There may be an efficient Ruby solution, but this is one tool which  
could be used for the job.

smitty wrote:

> **···**
>
> > Is there a more efficient way to convert a string such as  
> > "&#97;&#98;&#99;&#100;&#101;&#102;" to "abcdef" ?
> > 
> > Currently I am doing the following:  
> > a="&#97;&#98;&#99;&#100;&#101;&#102;"  
> > a.delete!("&#")  
> > ar = a.split(';')  
> > arn = ar.collect { |s| Integer(s) }  
> > s = arn.pack("U\*")  
> > puts "s=#{s.inspect}"
> > 
> > I need to do this for thousands of records that are  
> > loaded into a database, so I'm a bit concerned  
> > about repeatedly executing all of the above code.  
> > Thanks

---

<div class="post-metadata">

**Author:** ![Mark\_Thomas2](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Mark\_Thomas2](https://rubytalk.org/u/Mark_Thomas2)\
**Post date:** [8 September 2008 15:33 UTC](https://rubytalk.org/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/48927/4 "2008-09-08T15:33:45Z")

</div>

CGI::unescapeHTML(str)

> **···**
>
> On Sep 7, 10:19 pm, smitty \<cleesmith2...@gmail.com\> wrote:
> 
> > Is there a more efficient way to convert a string such as  
> > "&#97;&#98;&#99;&#100;&#101;&#102;" to "abcdef" ?

---

<div class="post-metadata">

**Author:** ![Bernard\_Kenik](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@Bernard\_Kenik](https://rubytalk.org/u/Bernard_Kenik)\
**Post date:** [8 September 2008 23:24 UTC](https://rubytalk.org/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/48927/5 "2008-09-08T23:24:14Z")

</div>

Mark Thomas wrote:

> **···**
>
> > On Sep 7, 10:19�pm, smitty \<cleesmith2...@gmail.com\> wrote:
> > 
> > > Is there a more efficient way to convert a string such as  
> > > "&#97;&#98;&#99;&#100;&#101;&#102;" to "abcdef" ?
> > 
> > CGI::unescapeHTML(str)
> 
> irb(main):029:0\> a="&#97;&#98;&#99;&#100;&#101;&#102;"  
> =\> "&#97;&#98;&#99;&#100;&#101;&#102;"  
> irb(main):030:0\> s = a.scan(/\d+/).collect { |ch| ch.to\_i.chr }.join()  
> =\> "abcdef"
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
