6
5

More than 3 years have passed since last update.

【Python】10進数を2進数、8進数、16進数に変換する

Posted at

サンプルコード

bin, oct, hexを使用する。

num = 100

# 2進数に変換
bin(num) # => '0b1100100'

# 8進数に変換
oct(num) # => '0o144'

# 16進数に変換
hex(num) # => '0x64'

もし0b, 0o, 0xのようなプレフィックスを除きたい場合は[2:]を末尾に追加すればいい。

num = 100

# 2進数に変換
bin(num)[2:] # => '1100100'

# 8進数に変換
oct(num)[2:] # => '144'

# 16進数に変換
hex(num)[2:] # => '64'

参考

6
5
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
6
5