Use bin
, ʻoct,
hex`.
num = 100
#Convert to binary
bin(num) # => '0b1100100'
#Convert to octal
oct(num) # => '0o144'
#Convert to hexadecimal
hex(num) # => '0x64'
If you want to remove prefixes like 0b
, 0o
, 0x
, add[2:]
to the end.
num = 100
#Convert to binary
bin(num)[2:] # => '1100100'
#Convert to octal
oct(num)[2:] # => '144'
#Convert to hexadecimal
hex(num)[2:] # => '64'
Recommended Posts