Beginner User having issue with converting char to ASCII

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

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

On Fri, Sep 12, 2008 at 6:40 PM, James G. [email protected]
wrote:

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

On Sep 12, 8:20 pm, Todd B. [email protected] wrote:

=> 97
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

James G. 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 G. II

Just adding this for the sake of being complete:

p ?A

=> 65

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

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

Aaron Burgess wrote:

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

http://DevMentor.org

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

Trick Nick wrote:

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! :slight_smile: