LoginSignup
2
0

More than 1 year has passed since last update.

2進数 ⇄ 10進数【Ruby】

Posted at

コーディングテストで出会った、rubyのオブジェクト変換メソッド(?、to_iやto_hなどの事を言いたい)の便利な仕様についてまとめる。

# 引数がinteger型の場合 → to_sメソッド
p 255.to_s(2)
# => "11111111"

# 引数がstring型の場合 → to_iメソッド
p "11111111".to_i(2)
# => 255

これらのメソッドは2進数以外の、任意の単位への変換も可能。

ex. )16進数の場合

p 255.to_s(16)
# => "ff"
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0