Skip to content

Instantly share code, notes, and snippets.

@kuredev
Last active April 11, 2020 14:40
Show Gist options
  • Save kuredev/a5bbd9255518bfe25d83d09946309b09 to your computer and use it in GitHub Desktop.
Save kuredev/a5bbd9255518bfe25d83d09946309b09 to your computer and use it in GitHub Desktop.
文字列を数値(N進数)に変換する
# 文字列を数値に変換する(N進数)
irb(main):111:0> "10".to_i
=> 10
irb(main):112:0> "10".to_i(2)
=> 2
irb(main):113:0> "10".to_i(16)
=> 16
irb(main):114:0> "6364".to_i(16)
=> 25444
# 数値を2進数(文字列型)に変換する
irb(main):012:0> 6.to_s # => 普通に10進数
=> "6"
irb(main):013:0> 6.to_s(2)
=> "110"
# 全部8bitの場合
> 6.to_s(2).rjust(8, "0")
=> "00000110"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment