Okay so I wanted to learn binary to write programs, so I could be able to say, "I can code in binary at 15 years old". I built this program but it prints the same incorrect values every time.
lowerCaseLetters = {
"a" => 01100001,
"b" => 01100010,
"c" => 01100011,
"d" => 01100100,
"e" => 01100101,
"f" => 01100110,
"g" => 01100111,
"h" => 01101000,
"i" => 01101001,
"j" => 01101010,
"k" => 01101011,
"l" => 01101100,
"m" => 01101101,
"n" => 01101110,
"o" => 01101111,
"p" => 01110000,
"q" => 01110001,
"r" => 01110010,
"s" => 01110011,
"t" => 01110100,
"u" => 01110101,
"v" => 01110110,
"w" => 01110111,
"x" => 01111000,
"y" => 01111001,
"z" => 01111010
}
puts "Type your message in English please, and \n -I the program will translate it into BINARY!!-"
userMessage = gets.chomp
puts "Your message in English says: \n >#{userMessage}"
lowerCaseLetters.each {
|letter, number|
print "#{number},"
}
So when I run this program no matter what I type, the program outputs these values. Even if I type in one value, or 6 values (thats the highest I've been up so far.... giggity)
Here are two examples from the terminal. Example 1:
Type your message in English please, and
-I the program will translate it into BINARY!!-
j
Your message in English says:
>j
294913,294920,294921,294976,294977,294984,294985,295424,295425,295432,295433,295488,295489,295496,295497,299008,299009,299016,299017,299072,299073,299080,299081,299520,299521,299528,
Example 2:
Type your message in English please, and
-I the program will translate it into BINARY!!-
jeebs
Your message in English says:
>jeebs
294913,294920,294921,294976,294977,294984,294985,295424,295425,295432,295433,295488,295489,295496,295497,299008,299009,299016,299017,299072,299073,299080,299081,299520,299521,299528,
Where and why am I having syntax errors? Thanks!!
Numbers beginning with 0 are reserved for octal representation so try this as a workaround:
lowerCaseLetters = { "a" => '01100001', "b" => '01100010',
"c" => '01100011', "d" => '01100100', "e" => '01100101',
"f" => '01100110', "g" => '01100111', "h" => '01101000',
"i" => '01101001', "j" => '01101010', "k" => '01101011',
"l" => '01101100', "m" => '01101101', "n" => '01101110',
"o" => '01101111', "p" => '01110000', "q" => '01110001',
"r" => '01110010', "s" => '01110011', "t" => '01110100',
"u" => '01110101', "v" => '01110110', "w" => '01110111',
"x" => '01111000', "y" => '01111001', "z" => '01111010'
}
puts "Type your message in English please,"
puts "and I the program will translate it into BINARY!!-"
userMessage = gets.chomp.split('')
puts "Your message in English says: "
userMessage.each {|letter| print lowerCaseLetters[letter] + " " }
puts
So inputting 'ab' returns: 01100001 01100010
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With