Dear all,
Iam in process of building a parser. I need to convert the numeric
character reference to corresponding ascii.
when numeric character reference like L or ÿ is given i need
to convert those into corresponding ascii. consider character reference
like L my parser will read character by character and eliminate &#;
and store ‘76’ alone in a string variable (temp), so now temp = ‘76’, i
need to generate its corresponding ascii value ‘L’. please help me to
the process
example: WEL after ASCII code conversion it should be WELCOME as
for 76 corresponding ascii value is ‘L’.
Like wise consider the second example ÿ
now temp = 00ff it should also generate its corresponding ascii code.
in both cases even if its a decimal value or hexa decimal value its
is stored as string in my process. As iam reading character by character
from buffer and adding each character to temp string.
so please help me to solve this issue…
Thanks in advance…
Regards,
Jose Martin
On May 30, 2008, at 2:32 AM, dare ruby wrote:
Dear all,
Iam in process of building a parser. I need to convert the numeric
character reference to corresponding ascii.
…
Regards,
Jose Martin
You might consider googling “lexical analysis” because the particular
thing you describe is often handled at an earlier point in the process
(even if only a few lines
In particular, /&#([0-9]+);/ and /
&#x([0-9A-Fa-f]+);/ are, as you said, handled slightly differently
even though they both involve a numeric to character representation.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
Hi,
2008/5/30 dare ruby [email protected]:
the process
from buffer and adding each character to temp string.
so please help me to solve this issue…
Thanks in advance…
irb(main):001:0> require ‘cgi’
=> true
irb(main):002:0> CGI.unescapeHTML(“WELCOME”)
=> “WELCOME”
irb(main):003:0> CGI.unescapeHTML(“WEÿCOME”)
=> “WE\377COME”
Regards,
Park H.
Heesob P. wrote:
irb(main):001:0> require ‘cgi’
=> true
irb(main):002:0> CGI.unescapeHTML(“WELCOME”)
=> “WELCOME”
irb(main):003:0> CGI.unescapeHTML(“WEÿCOME”)
=> “WE\377COME”
Actually CGI.escape and CGI.unescape are such short methods that you can
copy and paste them from the Ruby source code rather than requiring the
whole CGI kit and caboodle. At least, that’s what I do. 
def URLencode(string)
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
‘%’ + $1.unpack(‘H2’ * $1.size).join(’%’).upcase
end.tr(’ ', ‘+’)
end
def URLdecode(string)
string.tr(’+’, ’ ‘).gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
[$1.delete(’%’)].pack(‘H*’)
end
end
On May 30, 4:32 pm, dare ruby [email protected] wrote:
the process
from buffer and adding each character to temp string.
so please help me to solve this issue…
Thanks in advance…
Regards,
Jose Martin
Posted viahttp://www.ruby-forum.com/.
‘76’.to_i.chr
Heesob P. wrote:
Hi,
2008/5/30 dare ruby [email protected]:
the process
from buffer and adding each character to temp string.
so please help me to solve this issue…
Thanks in advance…
irb(main):001:0> require ‘cgi’
=> true
irb(main):002:0> CGI.unescapeHTML(“WELCOME”)
=> “WELCOME”
irb(main):003:0> CGI.unescapeHTML(“WEÿCOME”)
=> “WE\377COME”
Regards,
Park H.
Dear Park H.,
thanks for your comments on my question. its working fine now but could
you specify what would be the range of decimal and hexa decimal
reference in your case. A link would be very helpful to me.
even though its working well iam not getting proper result after 255 in
decimal like , Ā its returning nul. so could you specify the range
for the character reference for decimal and hexadecimal in your case.
Thanks in advance…
Regards,
Jose
end
I guess you need to set $KCODE = “U” for greater than 255.
Regards,
Park H.
Dear Park,
Thanks for your information. its very useful for me but is it possible
to do the same with out using CGI. I want to do without using CGI so
could you suggest?
regards,
Jose
Hi,
2008/6/2 dare ruby [email protected]:
for the character reference for decimal and hexadecimal in your case.
Take a look at cgi.rb. It is easy to understand.
Here is the snippet:
def CGI::unescapeHTML(string)
string.gsub(/&(amp|quot|gt|lt|#[0-9]+|#x[0-9A-Fa-f]+);/n) do
match = $1.dup
case match
when ‘amp’ then ‘&’
when ‘quot’ then ‘"’
when ‘gt’ then ‘>’
when ‘lt’ then ‘<’
when /\A#0*(\d+)\z/n then
if Integer($1) < 256
Integer($1).chr
else
if Integer($1) < 65536 and ($KCODE[0] == ?u or $KCODE[0] ==
?U)
[Integer($1)].pack(“U”)
else
“&##{$1};”
end
end
when /\A#x([0-9a-f]+)\z/ni then
if $1.hex < 256
$1.hex.chr
else
if $1.hex < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
[$1.hex].pack(“U”)
else
“&#x#{$1};”
end
end
else
“&#{match};”
end
end
end
I guess you need to set $KCODE = “U” for greater than 255.
Regards,
Park H.
Of course you don’t need to require cgi.
Just define your own method and paste code from cgi.rb
Regards,
Park H.
Dear Park,
Thanks for your immediate response. Actually i need to generate the
ascii code of the hexadecimal value i got.
Suppose i have a string value = “ÿ”, please help me to generate the
ascii code of the above hexadecimal value.
please look at the following link,
so the ascii code for the value (ÿ) is 255. so please help me to
the ascii code.
Regards,
jose
so the ascii code for the value (ÿ) is 255. so please help me to
the ascii code.
Regards,
jose
I got the ascii code of hexa decimal value using
value.hex.chr
I was having hexadecimal string like value = “ÿ” and i have take
only the string “ff” from the value so now val=“ff”
now,
puts val.hex.chr
==> 255
Thanks for everyone, specially park for your immediate response
regards,
Jose
What is the best book for an experienced programmer (Perl, C#,
VisualBasic, APL, A+, Assembler) to
use to jump into Ruby?
Joe
dare ruby wrote:
end
I guess you need to set $KCODE = “U” for greater than 255.
Regards,
Park H.
Dear Park,
Thanks for your information. its very useful for me but is it possible
to do the same with out using CGI. I want to do without using CGI so
could you suggest?
Of course you don’t need to require cgi.
Just define your own method and paste code from cgi.rb
Regards,
Park H.
On Tue, Jun 3, 2008 at 12:37 PM, joseph collins [email protected]
wrote:
What is the best book for an experienced programmer (Perl, C#, VisualBasic,
APL, A+, Assembler) to
use to jump into Ruby?
Joe
Two really,
“The Ruby P.ming Language” by David Flanigan and Matz, published by
O’Reilly is probably the best reference on the language itself, although
it
doesn’t really go into the standard class library. It covers both Ruby
1.8
and 1.9.
The classic is “Programming Ruby” by Dave T. et. al. It has more in
the
way of tutorials and covers the standard classes as well as the
language.
It’s currently available in print as the second edition, but the third
which
covers Ruby 1.9 is in preparation and is available via the Pragmatic
Programmers beta program as a PDF now updated periodically with the
option
to pre-order the paper version at a discount.
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
What is the best book for an experienced programmer (Perl, C#,
VisualBasic, APL, A+, Assembler) to
use to jump into Ruby?
Joe