Decimal to binary conversion, using BC Math.
Note: this function is VERY slow if the decimal number is too big!
<?
function bc_decbin($dec_str) {
if (strlen($dec_str)>0) {
$bin_str = '';
do {
if (((int)$dec_str[strlen($dec_str)-1] % 2) === 0)
$bin_str .= '0';
else
$bin_str .= '1';
$dec_str = bcdiv($dec_str, '2');
} while ($dec_str!='0');
return strrev($bin_str);
}
else
return null;
}
?>
-----
Cristian
www.CodeFlower.com
bcdiv
(PHP 4, PHP 5)
bcdiv — 2つの任意精度数値で除算を行う
説明
string bcdiv ( string left_operand, string right_operand [, int scale] )left_operand を right_operand で除算します。
パラメータ
- left_operand
左オペランドを表す文字列。
- right_operand
右オペランドを表す文字列。
- scale
このオプションパラメータを使用して、 結果の小数点以下の桁数を指定します。すべての関数で使用するデフォルトのスケールを定義するには bcscale() を使用します。
返り値
除算結果を文字列で返します。 right_operand が 0 の場合は NULL を返します。
例
参考
| bcmul() |
bcdiv
cristianDOTzuddas]NOSPAM[gmailDOTcom
24-Jul-2005 09:10
24-Jul-2005 09:10