質問
このページをwikiページした一般からの変換は、単一のcharをascii http://en.wikibooks.org/wiki/Ruby_Programming/ASCII
ものの言している場合は文字列としたいと思ってい各文字のasciiかになるのか?
"string".each_byte do |c|
$char = c.chr
$ascii = ?char
puts $ascii
end
この値は管理画面より変更可能でないのは嬉しい線$ascii=?char
syntax error, unexpected '?'
$ascii = ?char
^
解決
c
変数にはすでにcharコードが含まれています!
"string".each_byte do |c|
puts c
end
利回り
115
116
114
105
110
103
他のヒント
puts "string".split('').map(&:ord).to_s
についてはこちらを参照くださいポストに変ruby1.9 取得ASCII文字コードのRubyでの使用`?` (マーク)に失敗した
Ruby Stringは、1.9.1以降の codepoints
メソッドを提供します。
str = 'hello world'
str.codepoints.to_a
=> [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
str = "你好世界"
str.codepoints.to_a
=> [20320, 22909, 19990, 30028]
単一の文字には" x" .ordを使用し、文字列全体には" xyz" .sumを使用します。
各_byteまたはさらに優れたString#bytesの後にto_aを呼び出すこともできます
=> 'hello world'.each_byte.to_a
=> [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
=> 'hello world'.bytes
=> [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
"a"[0]
または
?a
どちらも同等のASCIIを返します。
所属していません StackOverflow