python, php, ruby How to convert decimal numbers to n-ary numbers

python

python


data = 255
#Binary number
b = bin(data) # '0b11111111'
b = format(data, 'b') # '11111111'
#8 base
o = oct(data) # '0377'
o = format(data, 'o') # '377'
#Hexadecimal
x = hex(data) # '0xff'
x = format(data, 'x') # 'ff'
#Convert 255 to decimal and write the last digit in decimal
int_x = int(format(data, 'x')[-1], 16) # 15

PHP

PHP


$data = 255;
//Binary number
$b = decbin($data); // '11111111'
//8 base
$o = decoct($data); // '377'
//Hexadecimal
$x = dechex($data); // 'ff'
//Convert 255 to decimal and write the last digit in decimal
$int_x = hexdec(substr(dechex($data), -1)); // 15

Ruby

Ruby


data = 255
#Binary number
b = data.to_s(2) # '11111111'
#8 base
o = data.to_s(8) # '377'
#Hexadecimal
x = data.to_s(16) # 'ff'
#Convert 255 to decimal and write the last digit in decimal
int_x = data.to_s(16)[-1].to_i(16) # 15

Recommended Posts

python, php, ruby How to convert decimal numbers to n-ary numbers
Convert decimal numbers to n-ary numbers [python]
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
How to convert floating point numbers to binary numbers in Python
How to handle JSON in Ruby, Python, JavaScript, PHP
[Python] Convert natural numbers to ordinal numbers
[Python] How to convert db file to csv
[Python] How to display random numbers (random module)
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
How to write Ruby to_s in Python
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
How to convert SVG to PDF and PNG [Python]
How to install Python
How to install python
How to convert Python # type for Python super beginners: str
Convert CIDR notation netmask to dotted decimal notation in Python
How to convert JSON file to CSV file with Python Pandas
How to add page numbers to PDF files (in Python)
Handle prime numbers in Python / Ruby / PHP / Golang (Go)
[2020.8 latest] How to install Python
[python] Convert date to string
How to install Python [Windows]
Convert numpy int64 to python int
python3: How to use bottle (2)
Java VS PHP VS Python VS Ruby
Cheating from PHP to Python
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
About Perl, Python, PHP, Ruby
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Python: How to use pydub
[Python] How to use checkio
Convert IP address to decimal
How to run Notepad ++ Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
Convert python 3.x code to python 2.x
How to use Python bytes
Don't lose to Ruby! How to run Python (Django) on Heroku
How to convert Youtube to mp3 and download it super-safely [Python]
How to convert Python # type for Python super beginners: int, float
Ruby vs Python Performance. How to pick the right technology?
How to call Python or Julia from Ruby (experimental implementation)
How to install python using anaconda
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
Offline real-time how to write E11 ruby and python implementation example
Convert markdown to PDF in Python
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series