[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers

Sample code

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'

reference

Recommended Posts

[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
Convert decimal numbers to n-ary numbers [python]
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
How to convert floating point numbers to binary numbers in Python
python, php, ruby How to convert decimal numbers to n-ary numbers
[Python] Convert natural numbers to ordinal numbers
Read big endian binary in Python and convert it to ndarray
Convert FBX files to ASCII <-> BINARY in Python
How to convert SVG to PDF and PNG [Python]
Convert timezoned date and time to Unixtime in Python2.7
[Python] Convert general-purpose container and class to each other
Convert CIDR notation netmask to dotted decimal notation in Python
Convert a text file with hexadecimal values to a binary file
C> Convert decimal to hexadecimal> Do not use sprintf ()
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert IP address to decimal
Convert python 3.x code to python 2.x
Display numbers and letters assigned to variables in python print
How to convert Youtube to mp3 and download it super-safely [Python]
Convert video to black and white with ffmpeg + python + opencv
Determine the date and time format in Python and convert to Unixtime
Python 3.6 on Windows ... and to Xamarin.
[Introduction to Python3 Day 1] Programming and Python
Convert markdown to PDF in Python
Workflow to convert formula (image) to python
Convert list to DataFrame with python
Python> list> Convert double list to single list
Python logging and dump to json
Convert the result of python optparse to dict and utilize it
Selenium and python to open google
Python> tuple> Convert double tuple to single tuple
[Python] Change the alphabet to numbers
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
How to put a half-width space before letters and numbers in Python.
Read CSV file with Python and convert it to DataFrame as it is
[Python] How to convert db file to csv
From Python to using MeCab (and CaboCha)
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
[Python] How to display random numbers (random module)
How to install and use pandas_datareader [Python]
Bind methods to Python classes and instances
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Convert psd file to png in Python
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
Convert from katakana to vowel kana [python]
Convert FX 1-minute data to 5-minute data with Python
python> Convert tuple to list> aList = list (pi_tuple)
Porting and modifying doublet-solver from python2 to python3.
Read Python csv and export to txt
python: How to use locals () and globals ()
Convert Python date types to RFC822 format
Python> Output numbers from 1 to 100, 501 to 600> For csv