Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Ruby)  »  Beginner User having issue with converting char to...
Page 1 of 1    

Beginner User having issue with converting char to...

Author Message
Nick Bo...
Posted: Sat Sep 13, 2008 3:25 am
Guest
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:Confused
#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-forum.com/.
 
James Gray...
Posted: Sat Sep 13, 2008 4:40 am
Guest
On Sep 12, 2008, at 5:25 PM, Nick Bo wrote:

Quote:
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:

Quote:
"a"[0]
=> 97


Hope that helps and welcome to Ruby!

James Edward Gray II
 
Todd Benson...
Posted: Sat Sep 13, 2008 5:20 am
Guest
On Fri, Sep 12, 2008 at 6:40 PM, James Gray <james at (no spam) grayproductions.net> wrote:
Quote:
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
 
Nick Bo...
Posted: Thu Sep 18, 2008 6:17 am
Guest
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-forum.com/.
 
Rajinder Yadav...
Posted: Fri Oct 30, 2009 5:17 am
Guest
Aaron Burgess wrote:
Quote:
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[i].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.
 
Greg Barozzi...
Posted: Fri Oct 30, 2009 5:17 am
Guest
Trick Nick wrote:
Quote:
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[i]}"
end


Welcome to Ruby! Smile
--
Posted via http://www.ruby-forum.com/.
 
Aaron Burgess...
Posted: Fri Oct 30, 2009 5:17 am
Guest
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

--
Posted via http://www.ruby-forum.com/.
 
 
Page 1 of 1    
All times are GMT
The time now is Thu Nov 26, 2009 6:30 am