convert ascii codes in a string to a string of characters

S

smitty

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
 
P

Peña, Botp

From: smitty [mailto:[email protected]]=20
# Is there a more efficient way to convert a string such as
# "abcdef" to "abcdef" ?
#=20
# Currently I am doing the following:
# a=3D"abcdef"
# a.delete!("&#")
# ar =3D a.split(';')
# arn =3D ar.collect { |s| Integer(s) }
# s =3D arn.pack("U*")
# puts "s=3D#{s.inspect}"

i'm feeling good today ;)
"abcdef".delete("&#").split(";").map{|x| =
x.to_i}.pack("U*")
=3D> "abcdef"

or
"abcdef".gsub(/\&\#(\d+);/){|x| =
$1.to_i.chr}
=3D> "abcdef"
require 'cgi' =3D> false
CGI.unescapeHTML "abcdef"
=3D> "abcdef"

kind regards -botp
 
B

Bernard Kenik

Mark said:
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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top