Ruby: character to ascii from a string

2020-02-07 17:07发布

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 ascii
7条回答
beautiful°
2楼-- · 2020-02-07 17:30
"a"[0]

or

?a

Both would return their ASCII equivalent.

查看更多
登录 后发表回答