Convert ascii codes in a string to a string of characters

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

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

···

From: smitty [mailto:cleesmith2006@gmail.com]
#
# 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 :wink:

"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

Have you looked at rubyinline | software projects | by ryan davis ?

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
"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

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" ?

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