how to generate ascii code?

D

dare ruby

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
 
P

Paul

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

'76'.to_i.chr
 
H

Heesob Park

Hi,

2008/5/30 dare ruby said:
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...

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 Heesob
 
R

Rob Biedenharn

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://agileconsultingllc.com
(e-mail address removed)
 
D

Dave Bass

Heesob said:
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
 
D

dare ruby

Heesob said:
Hi,



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 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 , Ā 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
 
H

Heesob Park

Hi,

2008/6/2 dare ruby said:
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 , Ā 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
 
D

dare ruby

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
 
H

Heesob Park

dare said:
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
 
D

dare ruby

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

please look at the following link,

http://www.ascii.cl/

so the ascii code for the value (ÿ) is 255. so please help me to
the ascii code.

Regards,
jose
 
D

dare ruby

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
 
J

joseph collins

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: (e-mail address removed)
Subject: Re: how to generate ascii code?
To: (e-mail address removed)
=20
=20
=20
I got the ascii code of hexa decimal value using
=20
value.hex.chr
=20
I was having hexadecimal string like value =3D "ÿ" and i have take=20
only the string "ff" from the value so now val=3D"ff"
=20
now,
puts val.hex.chr
=20
=3D=3D> 255
=20
Thanks for everyone, specially park for your immediate response
=20
regards,
Jose
=20
--=20
Posted via http://www.ruby-forum.com/.
=20

_________________________________________________________________
Now you can invite friends from Facebook and other groups to join you on Wi=
ndows Live=99 Messenger. Add now.
https://www.invite2messenger.net/im/?source=3DTXT_EML_WLH_AddNow_Now=
 
J

joseph collins

What is the best book for an experienced programmer (Perl, C#, VisualBasic,=
APL, A+, Assembler) to
use to jump into Ruby?
=20
Joe
Date: Wed, 4 Jun 2008 01:15:37 +0900
From: (e-mail address removed)
Subject: Re: how to generate ascii code?
To: (e-mail address removed)
=20
=20
What is the best book for an experienced programmer (Perl, C#, VisualBasi= c, APL, A+, Assembler) to
use to jump into Ruby?
=20
Joe
=20
=20
_________________________________________________________________
Now you can invite friends from Facebook and other groups to join you on =
Windows Live=99 Messenger. Add now.

_________________________________________________________________
Now you can invite friends from Facebook and other groups to join you on Wi=
ndows Live=99 Messenger. Add now.
https://www.invite2messenger.net/im/?source=3DTXT_EML_WLH_AddNow_Now=
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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

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
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top