サンプルコード
<?php
/**
* 10進数→2進数
* 引数:対象
* 戻値:結果
*/
function _chgDecToBin($p_sValue) {
if (($p_sValue === '0') || ($p_sValue === '-0')) {
return '0';
}
$arrNumber = array('0' => '0', '1' => '1');
$sNumber = '2';//2進数
$sConvert = '';
while ($p_sValue !== '0') {
$sConvert = $arrNumber[bcmod($p_sValue, $sNumber)].$sConvert;
$p_sValue = bcdiv($p_sValue, $sNumber, 0);
if ($p_sValue === null) {
break;
}
}
return $sConvert;
}
var_dump(_chgDecToBin('10'));
※結果
string(4) "1010"
?>
<?php
/**
* 2進数→10進数
* 引数:対象
* 戻値:結果
*/
function _chgBinToDec($p_sValue) {
if (($p_sValue === '0') || ($p_sValue === '-0')) {
return '0';
}
$arrNumber = array('0' => '0', '1' => '1');
$sNumber = '2';//2進数
$sConvert = '0';
$p_sValue = strrev($p_sValue);
$nLen = strlen($p_sValue);
for($nIdx = 0; $nIdx < $nLen; $nIdx++) {
$sBcpow = bcpow($sNumber, $nIdx, 0);
$sBcmul = bcmul($sBcpow, $arrNumber[$p_sValue{$nIdx}]);
$sConvert = bcadd($sBcmul, $sConvert);
}
return $sConvert;
}
var_dump(_chgBinToDec('1010'));
※結果
string(2) "10"
?>
<?php
/**
* 2進数 → 8進数
* 引数:対象
* 戻値:結果
*/
function _chgBinToOct($p_sValue) {
$arrNumber = array('000' => '0', '001' => '1', '010' => '2', '011' => '3', '100' => '4', '101' => '5', '110' => '6', '111' => '7');
$nBit = 3;//3bit
$sConvert = '';
$nLen = strlen($p_sValue);
$nLoop = ceil($nLen / $nBit);
$p_sValue = str_pad($p_sValue, $nLoop * $nBit, '0', STR_PAD_LEFT);
for($nIdx=0; $nIdx<$nLoop; $nIdx++) {
$sSubstr = substr($p_sValue, $nIdx * $nBit, $nBit);
$sConvert .= $arrNumber[$sSubstr];
}
return $sConvert;
}
var_dump(_chgBinToOct('1010'));
※結果
string(2) "12"
?>
<?php
/**
* 8進数 → 2進数
* 引数:対象
* 戻値:結果
*/
function _chgOctToBin($p_sValue) {
$arrNumber = array('0' => '000', '1' => '001', '2' => '010', '3' => '011', '4' => '100', '5' => '101', '6' => '110', '7' => '111');
$sConvert = '';
$nLen = strlen($p_sValue);
for($nIdx=0; $nIdx<$nLen; $nIdx++) {
$sSubstr = substr($p_sValue, $nIdx, 1);
$sConvert .= $arrNumber[$sSubstr];
}
return $sConvert;
}
var_dump(_chgOctToBin('12'));
※結果
string(6) "001010"
?>
<?php
/**
* 2進数 → 16進数
* 引数:対象
* 戻値:結果
*/
function _chgBinToHex($p_sValue) {
$arrNumber = array('0000' => '0', '0001' => '1', '0010' => '2', '0011' => '3', '0100' => '4', '0101' => '5', '0110' => '6', '0111' => '7',
'1000' => '8', '1001' => '9', '1010' => 'A', '1011' => 'B', '1100' => 'C', '1101' => 'D', '1110' => 'E', '1111' => 'F');
$nBit = 4;//4bit
$sConvert = '';
$nLen = strlen($p_sValue);
$nLoop = ceil($nLen / $nBit);
$p_sValue = str_pad($p_sValue, $nLoop * $nBit, '0', STR_PAD_LEFT);
for($nIdx=0; $nIdx<$nLoop; $nIdx++) {
$sSubstr = substr($p_sValue, $nIdx * $nBit, $nBit);
$sConvert .= $arrNumber[$sSubstr];
}
return $sConvert;
}
var_dump(_chgBinToHex('1010'));
※結果
string(1) "A"
?>
<?php
/**
* 16進数 → 2進数
* 引数:対象
* 戻値:結果
*/
function _chgHexToBin($p_sValue) {
$arrNumber = array('0' => '0000', '1' => '0001', '2' => '0010', '3' => '0011', '4' => '0100', '5' => '0101', '6' => '0110', '7' => '0111',
'8' => '1000', '9' => '1001', 'A' => '1010', 'B' => '1011', 'C' => '1100', 'D' => '1101', 'E' => '1110', 'F' => '1111');
$sConvert = '';
$nLen = strlen($p_sValue);
for($nIdx=0; $nIdx<$nLen; $nIdx++) {
$sSubstr = substr($p_sValue, $nIdx, 1);
$sConvert .= $arrNumber[$sSubstr];
}
return $sConvert;
}
var_dump(_chgHexToBin('A'));
※結果
string(4) "1010"
?>
<?php
/**
* 2の補数
* 引数:対象
* 戻値:結果
*/
function _chgTwosComplement($p_sValue) {
$sReverse = '';
$nLen = strlen($p_sValue);
for ($nIdx=0; $nIdx<$nLen; $nIdx++) {
if ($p_sValue[$nIdx] === '0') {
$sReverse = $sReverse.'1';
} else {
$sReverse = $sReverse.'0';
}
}
$sConvert = '';
$sReverse = strrev($sReverse);
$nLen = strlen($sReverse);
$sAdd = '1';
for ($nIdx=0; $nIdx<$nLen; $nIdx++) {
if (($sReverse[$nIdx] === '0') && ($sAdd === '1')) {
$sAdd = '';
$sConvert = '1'.$sConvert;
} else if (($sReverse[$nIdx] === '1') && ($sAdd === '1')) {
$sAdd = '1';
$sConvert = '0'.$sConvert;
} else {
$sConvert = $sReverse[$nIdx].$sConvert;
}
}
return $sConvert;
}
var_dump(_chgTwosComplement('1010'));
※結果
string(4) "0110"
?>