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

**URL:** <https://www.ruby-forum.com/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/147990>\
**Category:** Ruby\
**Created:** [September 8, 2008, 4:58am UTC](https://www.ruby-forum.com/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/147990 "2008-09-08T04:58:41Z")\
**Posts on this page:** 5\
**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:** [September 8, 2008, 4:58am UTC](https://www.ruby-forum.com/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/147990/1 "2008-09-08T04:58:41Z")

</div>

Is there a more efficient way to convert a string such as  
“abcdef” to “abcdef” ?

Currently I am doing the following:  
a=“abcdef”  
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:** ![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:** [September 8, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/147990/2 "2008-09-08T16:07:56Z")

</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.

---

<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:** [September 8, 2008, 5:02am UTC](https://www.ruby-forum.com/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/147990/3 "2008-09-08T05:02:47Z")

</div>

From: smitty [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Is there a more efficient way to convert a string such as

# “abcdef” to “abcdef” ?

# 

# Currently I am doing the following:

# a=“abcdef”

# a.delete!(“&#”)

# ar = a.split(‘;’)

# arn = ar.collect { |s| Integer(s) }

# s = arn.pack(“U\*”)

# puts “s=#{s.inspect}”

i’m feeling good today 😉

> “abcdef”.delete(“&#”).split(“;”).map{|x| x.to\_i}.pack(“U\*”)  
> =\> “abcdef”

or

> “abcdef”.gsub(/&#(\d+);/){|x| $1.to\_i.chr}  
> =\> “abcdef”

> require ‘cgi’  
> =\> false  
> CGI.unescapeHTML “abcdef”  
> =\> “abcdef”

kind regards -botp

---

<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:** [September 8, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/147990/4 "2008-09-08T17:43:19Z")

</div>

On Sep 7, 10:19 pm, smitty [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a more efficient way to convert a string such as  
> “abcdef” to “abcdef” ?

CGI::unescapeHTML(str)

---

<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:** [September 9, 2008, 1:30am UTC](https://www.ruby-forum.com/t/convert-ascii-codes-in-a-string-to-a-string-of-characters/147990/5 "2008-09-09T01:30:29Z")

</div>

Mark T. wrote:

> On Sep 7, 10:19ï¿½pm, smitty [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Is there a more efficient way to convert a string such as  
> > “abcdef” to “abcdef” ?
> 
> CGI::unescapeHTML(str)

irb(main):029:0\> a=“abcdef”  
=\> “abcdef”  
irb(main):030:0\> s = a.scan(/\d+/).collect { |ch| ch.to\_i.chr }.join()  
=\> “abcdef”
