this wiki page gave a general idea of how to convert a single char to ascii http://en.wikibooks.org/wiki/Ruby_Programming/ASCII
But say if I have a string and I wanted to get each character's ascii from it, what do i need to do?
"string".each_byte do |c|
$char = c.chr
$ascii = ?char
puts $ascii
end
It doesn't work because it's not happy with the line $ascii = ?char
syntax error, unexpected '?'
$ascii = ?char
^
Ruby String provides the
codepoints
method after 1.9.1.use "x".ord for a single character or "xyz".sum for a whole string.
The
c
variable already contains the char code!yields
please refer to this post for the changes in ruby1.9 Getting an ASCII character code in Ruby using `?` (question mark) fails
You could also just call to_a after each_byte or even better String#bytes