10
13

More than 5 years have passed since last update.

python, php, ruby 10進数をn進数に変換する方法

Last updated at Posted at 2015-08-13

python

python
data = 255
# 2進数
b = bin(data) # '0b11111111'
b = format(data, 'b') # '11111111'
# 8進数
o = oct(data) # '0377'
o = format(data, 'o') # '377'
# 16進数
x = hex(data) # '0xff'
x = format(data, 'x') # 'ff'
# 255を16進数に変換し、下1桁を10進数に表記
int_x = int(format(data, 'x')[-1], 16) # 15

PHP

PHP
$data = 255;
// 2進数
$b = decbin($data); // '11111111'
// 8進数
$o = decoct($data); // '377'
// 16進数
$x = dechex($data); // 'ff'
// 255を16進数に変換し、下1桁を10進数に表記
$int_x = hexdec(substr(dechex($data), -1)); // 15

Ruby

Ruby
data = 255
# 2進数
b = data.to_s(2) # '11111111'
# 8進数
o = data.to_s(8) # '377'
# 16進数
x = data.to_s(16) # 'ff'
# 255を16進数に変換し、下1桁を10進数に表記
int_x = data.to_s(16)[-1].to_i(16) # 15
10
13
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
10
13