How to generate ascii code?

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-forum.com/.

'76'.to_i.chr

···

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-forum.com/.

Hi,

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

···

2008/5/30 dare ruby <martin@angleritech.com>:

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 :wink: 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://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

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

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

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-forum.com/\.

Heesob Park wrote:

Hi,

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

···

2008/5/30 dare ruby <martin@angleritech.com>:

--
Posted via http://www.ruby-forum.com/\.

Hi,

Heesob Park wrote:

Hi,

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

···

2008/6/2 dare ruby <martin@angleritech.com>:

2008/5/30 dare ruby <martin@angleritech.com>:

  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-forum.com/\.

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-forum.com/\.

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,

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

Regards,
jose

···

--
Posted via http://www.ruby-forum.com/\.

so the ascii code for the value (&#xff:wink: 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-forum.com/\.

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 (&#xff:wink: 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-forum.com/\.

_________________________________________________________________
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 (&#xff:wink: 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-forum.com/\.
>

_________________________________________________________________
Now you can invite friends from Facebook and other groups to join you on Windows Live™ Messenger. Add now.
The extent of social media's impact on society is inconceivable.

_________________________________________________________________
Now you can invite friends from Facebook and other groups to join you on Windows Live™ Messenger. Add now.

Two really,

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

Joe

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/