[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to generate ascii code?

dare ruby

5/30/2008 6:32:00 AM

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 via http://www.ruby-....

13 Answers

Paul

5/30/2008 7:45:00 AM

0

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 &#76; or &#x00ff; is given i need
> to convert those into corresponding ascii. consider character reference
> like &#76; 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: WE&#76; after ASCII code conversion it should be WELCOME as
> for 76 corresponding ascii value is 'L'.
>
> Like wise consider the second example &#x00ff;
>
> 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-....

'76'.to_i.chr

Heesob Park

5/30/2008 8:01:00 AM

0

Hi,

2008/5/30 dare ruby <martin@angleritech.com>:
> 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 &#76; or &#x00ff; is given i need
> to convert those into corresponding ascii. consider character reference
> like &#76; 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: WE&#76; after ASCII code conversion it should be WELCOME as
> for 76 corresponding ascii value is 'L'.
>
> Like wise consider the second example &#x00ff;
>
> 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...
>

irb(main):001:0> require 'cgi'
=> true
irb(main):002:0> CGI.unescapeHTML("WE&#76;COME")
=> "WELCOME"
irb(main):003:0> CGI.unescapeHTML("WE&#x00ff;COME")
=> "WE\377COME"


Regards,

Park Heesob

Rob Biedenharn

5/30/2008 2:11:00 PM

0

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 Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Dave Bass

5/30/2008 5:09:00 PM

0

Heesob Park wrote:
> irb(main):001:0> require 'cgi'
> => true
> irb(main):002:0> CGI.unescapeHTML("WE&#76;COME")
> => "WELCOME"
> irb(main):003:0> CGI.unescapeHTML("WE&#x00ff;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

--
Posted via http://www.ruby-....

dare ruby

6/2/2008 5:29:00 AM

0

Heesob Park wrote:
> Hi,
>
> 2008/5/30 dare ruby <martin@angleritech.com>:
>> 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("WE&#76;COME")
> => "WELCOME"
> irb(main):003:0> CGI.unescapeHTML("WE&#x00ff;COME")
> => "WE\377COME"
>
>
> Regards,
>
> Park Heesob

Dear Park Heesob,

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 , &#256; 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
--
Posted via http://www.ruby-....

Heesob Park

6/2/2008 5:50:00 AM

0

Hi,

2008/6/2 dare ruby <martin@angleritech.com>:
> Heesob Park wrote:
>> Hi,
>>
>> 2008/5/30 dare ruby <martin@angleritech.com>:
>>> 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("WE&#76;COME")
>> => "WELCOME"
>> irb(main):003:0> CGI.unescapeHTML("WE&#x00ff;COME")
>> => "WE\377COME"
>>
>>
>> Regards,
>>
>> Park Heesob
>
> Dear Park Heesob,
>
> 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 , &#256; 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.


Regards,

Park Heesob

dare ruby

6/2/2008 6:46:00 AM

0


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

regards,
Jose
--
Posted via http://www.ruby-....

Heesob Park

6/2/2008 6:57:00 AM

0

dare ruby wrote:
>
>> end
>>
>> 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

Regards,
Park Heesob

--
Posted via http://www.ruby-....

dare ruby

6/3/2008 10:56:00 AM

0


> Of course you don't need to require cgi.
> Just define your own method and paste code from cgi.rb
>
> Regards,
> Park Heesob

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 = "&#xff;", please help me to generate the
ascii code of the above hexadecimal value.

please look at the following link,

http://www...

so the ascii code for the value (&#xff;) is 255. so please help me to
the ascii code.

Regards,
jose
--
Posted via http://www.ruby-....

dare ruby

6/3/2008 11:22:00 AM

0


>
> so the ascii code for the value (&#xff;) 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 = "&#xff;" 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-....