Beginner User having issue with converting char to ASCII

N

Nick Bo

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
 
T

Todd Benson

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:
=> 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
 
R

RichardOnRails

On Sep 12, 2008, at 5:25 PM, Nick Bo wrote:
In Ruby 1.8 you can just index into the String and Ruby will return the
character value:
=> 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.org/271697 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
 
N

Nick Bo

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} \t \t #{asciiArray} \n"
i=i+1
end
 
A

Aaron Burgess

This is much easier and more efficient than of the suggestions on this
site. You should not have to index anything.

letters =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
character = letters.split(//)
ascii=[]
character.each do |a|
ascii << "#{a} \t #{a[0]}"
end
puts ascii

- aaron burgess
 
R

Rajinder Yadav

Aaron said:
This is much easier and more efficient than of the suggestions on this
site. You should not have to index anything.

letters =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
character = letters.split(//)
ascii=[]
character.each do |a|
ascii << "#{a} \t #{a[0]}"
end
puts ascii

- aaron burgess

You could also reduce the number of loops from 2xO(n) to O(n) with

word="hello world!"
letters=[]

for i in 0...word.size # note use of 3 dots for exclusive range
letters << word.chr
end

p letters

=>["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", "!"]


--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives.
 
G

Greg Barozzi

Trick said:
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.

This way works as well and is shorter:

# set up the array
arr = (('0'..'9').map + ('A'..'Z').map + ('a'..'z').map).flatten

# convert the array to a string
str = arr.to_s

# print the ascii table
str.length.times do |i|
puts "#{str[i,1]} \t #{str}"
end


Welcome to Ruby! :)
 

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
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top