[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Beginner User having issue with converting char to ASCII

Nick Bo

9/12/2008 10:25:00 PM

I am working on this assignment and this is the first class I have used
ruby with so please forgive if some of these questions seem elementary.

Basically the job is to take a string with characters "a-zA-Z0-9" and
then turn it into an array using the .split method. Then do an "each
do" loop that will run through each array of the character array and
find the ASCII value. my issue is I dont know how to take the character
and convert it into ASCII.

Here is a copy of what i got so far::?
#set up first array
characters = 'a b c d e f g h i j k l m n o p q r s t u v w x y z A B C
D E F ...G ...H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8
9'
characterArray = characters.split

#set up next array by using a loop to determine the ASCII value of each
...character

characterArray.each do |ascii|
print "#{ascii} \n"
end
--
Posted via http://www.ruby-....

5 Answers

James Gray

9/12/2008 11:40:00 PM

0

On Sep 12, 2008, at 5:25 PM, Nick Bo wrote:

> my issue is I dont know how to take the character
> and convert it into ASCII.

In Ruby 1.8 you can just index into the String and Ruby will return
the character value:

>> "a"[0]
=> 97

Hope that helps and welcome to Ruby!

James Edward Gray II

Todd Benson

9/13/2008 12:21:00 AM

0

On Fri, Sep 12, 2008 at 6:40 PM, James Gray <james@grayproductions.net> wrote:
> On Sep 12, 2008, at 5:25 PM, Nick Bo wrote:
>
>> my issue is I dont know how to take the character
>> and convert it into ASCII.
>
> In Ruby 1.8 you can just index into the String and Ruby will return the
> character value:
>
>>> "a"[0]
> => 97
>
> Hope that helps and welcome to Ruby!

Also, if you are required to iterate, you can do String#each_byte
after an Array#join, but I don't think that's what you are looking
for, but I couldn't resist (I know I'm a clown)...

[('a'..'z'),('A'..'Z'),('0'..'9')].inject([]){|s,i|s+i.map}.join.each_byte{|b|p
b}

If you need to do an each/do, you should probably do as James suggests.

Todd

RichardOnRails

9/13/2008 6:44:00 AM

0

On Sep 12, 8:20 pm, Todd Benson <caduce...@gmail.com> wrote:
> On Fri, Sep 12, 2008 at 6:40 PM, James Gray <ja...@grayproductions.net> wrote:
> > On Sep 12, 2008, at 5:25 PM, Nick Bo wrote:
>
> >> my issue is I dont know how to take the character
> >> and convert it into ASCII.
>
> > In Ruby 1.8 you can just index into the String and Ruby will return the
> > character value:
>
> >>> "a"[0]
> > => 97
>
> > Hope that helps and welcome to Ruby!
>
> Also, if you are required to iterate, you can do String#each_byte
> after an Array#join, but I don't think that's what you are looking
> for, but I couldn't resist (I know I'm a clown)...
>
> [('a'..'z'),('A'..'Z'),('0'..'9')].inject([]){|s,i|s+i.map}.join.each_byte{|b|p
> b}
>
> If you need to do an each/do, you should probably do as James suggests.
>
> Todd

Hi Nick,

James and Todd gave you good ideas. But because you're a newbie, you
may need a solution that exactly meets your stated requirements.

Your explicit coding of a string of the required character using a
blank separator is fine (except for the embedded ellipses [...]). The
"Ruby Way" is to generate such things.

http://www.pastie.... offers two solutions: A succinct version
(12 lines separated by three blank lines) and an instrument version
(23 lines).

The succinct version shows:

Section 1 (Line 1): Define “array” containing a reference to an Array
object,
which in turn contains references to three Range objects that contain
respectively single-letter strings: lower-case letters, upper-case
letters and decimal-digit characters.

Section 2 (Lines 3-8) defines an initially empty string named
“characters”. We successively process each range, In processing a
range, we process each one-character string contained therein appended
the string’s content to “characters:”.

Section 3 (Line 10) splits the “characters” string using the nil
string (‘’), which creates an array of all the one-character strings
and puts a reference to it in split_chs.

Section 4 (Lines 12-14) processes each one-character string in the
array referenced by “split_chs”. The character and its numeric code
representing it is displayed as a line of output. Altogether, 62
lines are generated. Line 15 provides an alternative to the coding of
line 13 if Ruby 1.9 or greater is being used.

The instrument version displays the intermediate values as they’re
computed. That may be helpful in understanding the code.

HTH,
Richard

Nick Bo

9/18/2008 2:53:00 AM

0

Thanks for all the help but with a little more research i found this to
be the easiest solution for my beginner knowledge thanks for all the
help.

#set up first array

characters = 'a b c d e f g h i j k l m n o p q r s t u v w x y z A B C
D E F G ...H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9'
characterArray = characters.split

#set up next array by using a loop to determine the ASCII value of each
...character

ascii=""
characterArray.each do |a|

ascii << "#{a.to_s[0]} \t"

end

#take the ascii string and then do a .split in order to build another
array.
asciiArray = ascii.split

#start the table format
print "Character \t ASCII \n"

#initialize i as 0 in order to go through each array and print the
character ...and ascii value.
i=0

#print each value and keep doing it until there is no more arrays for
...asciiArray (would work for characterArray as well)
asciiArray.each do
print "#{characterArray[i]} \t \t #{asciiArray[i]} \n"
i=i+1
end
--
Posted via http://www.ruby-....

Lloyd Linklater

9/18/2008 1:03:00 PM

0

James Gray wrote:
> On Sep 12, 2008, at 5:25 PM, Nick Bo wrote:
>
>> my issue is I dont know how to take the character
>> and convert it into ASCII.
>
> In Ruby 1.8 you can just index into the String and Ruby will return
> the character value:
>
> >> "a"[0]
> => 97
>
> Hope that helps and welcome to Ruby!
>
> James Edward Gray II

Just adding this for the sake of being complete:

p ?A

=> 65
--
Posted via http://www.ruby-....