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.
On May 30, 4:32 pm, dare ruby <mar...@angleritech.com> wrote:
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
--
Posted viahttp://www.ruby-forum.com/.
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.
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.
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
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 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.
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.
I guess you need to set $KCODE = "U" for greater than 255.
Regards,
Park Heesob
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?
I guess you need to set $KCODE = "U" for greater than 255.
Regards,
Park Heesob
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
_________________________________________________________________
Now you can invite friends from Facebook and other groups to join you on Windows Live™ Messenger. Add now.
What is the best book for an experienced programmer (Perl, C#, VisualBasic, APL, A+, Assembler) to
use to jump into Ruby?
Joe
···
Date: Wed, 4 Jun 2008 01:15:37 +0900
From: joec_49@hotmail.com
Subject: Re: how to generate ascii code?
To: ruby-talk@ruby-lang.org
What is the best book for an experienced programmer (Perl, C#, VisualBasic, APL, A+, Assembler) to
use to jump into Ruby?
Joe
> Date: Tue, 3 Jun 2008 20:21:49 +0900
> From: martin@angleritech.com
> Subject: Re: how to generate ascii code?
> To: ruby-talk@ruby-lang.org
>
>
> >
> > 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
>
> --
> Posted via http://www.ruby-forum.com/\.
>
_________________________________________________________________
Now you can invite friends from Facebook and other groups to join you on Windows Live™ Messenger. Add now.
"The Ruby Programming 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 Thomas 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.
···
On Tue, Jun 3, 2008 at 12:37 PM, joseph collins <joec_49@hotmail.com> wrote:
What is the best book for an experienced programmer (Perl, C#, VisualBasic,
APL, A+, Assembler) to
use to jump into Ruby?