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

Have you looked at 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.

From: smitty [mailto:[email protected]]

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

On Sep 7, 10:19 pm, smitty [email protected] wrote:

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

CGI::unescapeHTML(str)

Mark T. wrote:

On Sep 7, 10:19�pm, smitty [email protected] 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”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs