收藏本站 收藏本站
积木网首页 - 软件测试 - 常用手册 - 站长工具 - 技术社区
首页 > PHP > PHP数学函数 > 正文

首页 - PHP - 数据库 - 操作系统 - 游戏开发 - JS - Android - MySql - Redis - MongoDB - Win8 - Shell编程 - DOS命令 - jQuery - CSS样式 - Python - Perl

Access - Oracle - DB2 - SQLServer - MsSql2008 - MsSql2005 - Sqlite - PostgreSQL - node.js - extjs - JavaScript vbs - Powershell - Ruby

PHP技巧 - PHP基础 - PHP实例 - PHP字符串 - PHP数据库函数 - Mysql函数 - PDO类库 - PHP错误处理 - PHP选项 - PHP函数库 - PHP数学函数 - PHP数组

PHP:bindec()的用法_Math函数

bindec

(PHP 4, PHP 5)

bindec — 二进制转换为十进制

说明

number bindec ( string $binary_string )

返回 binary_string 参数所表示的二进制数的十进制等价值。

bindec() 将一个二进制数转换成 integer,或者出于大小的需要,转换为 float 类型。

bindec() 将所有的 binary_string 值解释为无符号整数。这是由于 bindec() 函数将其最高有效位视为数量级而非符号位。

参数

binary_string

要转换的二进制字符串

Warning

参数必须为字符串。使用其他数据类型会导致不可预知的结果。

返回值

binary_string 的十进制数值

更新日志

版本 说明 4.1.0 本函数如今可以转换超过程序运行平台中 integer 类型最大值的数,此时其值会返回为 float 类型。

范例

Example #1 bindec() 例子

<?php
echo bindec('110011') . "n";
echo bindec('000110011') . "n";

echo bindec('111');
?>

以上例程会输出:

51
51
7

Example #2 bindec() 将输入解读为无符号整数

<?php
/*
 * The lesson from this example is in the output
 * rather than the PHP code itself.
 */

$magnitude_lower = pow(2, (PHP_INT_SIZE * 8) - 2);
p($magnitude_lower - 1);
p($magnitude_lower, 'See the rollover?  Watch it next time around...');

p(PHP_INT_MAX, 'PHP_INT_MAX');
p(~PHP_INT_MAX, 'interpreted to be one more than PHP_INT_MAX');

if (PHP_INT_SIZE == 4) {
    $note = 'interpreted to be the largest unsigned integer';
} else {
    $note = 'interpreted to be the largest unsigned integer
              (18446744073709551615) but skewed by float precision';
}
p(-1, $note);


function p($input, $note = '') {
    echo "input:        $inputn";

    $format = '%0' . (PHP_INT_SIZE * 8) . 'b';
    $bin = sprintf($format, $input);
    echo "binary:       $binn";

    ini_set('precision', 20);  // For readability on 64 bit boxes.
    $dec = bindec($bin);
    echo 'bindec():     ' . $dec . "n";

    if ($note) {
        echo "NOTE:         $noten";
    }

    echo "n";
}
?>

以上例程在 32 位机器上的输出:

input:        1073741823
binary:       00111111111111111111111111111111
bindec():     1073741823
input:        1073741824
binary:       01000000000000000000000000000000
bindec():     1073741824
NOTE:         See the rollover?  Watch it next time around...
input:        2147483647
binary:       01111111111111111111111111111111
bindec():     2147483647
NOTE:         PHP_INT_MAX
input:        -2147483648
binary:       10000000000000000000000000000000
bindec():     2147483648
NOTE:         interpreted to be one more than PHP_INT_MAX
input:        -1
binary:       11111111111111111111111111111111
bindec():     4294967295
NOTE:         interpreted to be the largest unsigned integer

以上例程在 64 位机器上的输出:

input:        4611686018427387903
binary:       0011111111111111111111111111111111111111111111111111111111111111
bindec():     4611686018427387903
input:        4611686018427387904
binary:       0100000000000000000000000000000000000000000000000000000000000000
bindec():     4611686018427387904
NOTE:         See the rollover?  Watch it next time around...
input:        9223372036854775807
binary:       0111111111111111111111111111111111111111111111111111111111111111
bindec():     9223372036854775807
NOTE:         PHP_INT_MAX
input:        -9223372036854775808
binary:       1000000000000000000000000000000000000000000000000000000000000000
bindec():     9223372036854775808
NOTE:         interpreted to be one more than PHP_INT_MAX
input:        -1
binary:       1111111111111111111111111111111111111111111111111111111111111111
bindec():     18446744073709551616
NOTE:         interpreted to be the largest unsigned integer
              (18446744073709551615) but skewed by float precision

参见

decbin() - 十进制转换为二进制 octdec() - 八进制转换为十进制 hexdec() - 十六进制转换为十进制 base_convert() - 在任意进制之间转换数字

PHP:cos()的用法_Math函数
cos(PHP4,PHP5)cos余弦说明floatcos(float$arg)cos()返回参数arg的余弦值。参数arg的单位为弧度。参数arg以弧度表示的角度返回值arg的余弦值范例Example#1cos()例子?php

PHP:cosh()的用法_Math函数
cosh(PHP4=4.1.0,PHP5)cosh双曲余弦说明floatcosh(float$arg)返回arg的双曲余弦值,定义为(exp(arg)+exp(-arg))/2。参数arg要处理的参数返回值arg的双曲余弦值。参见cos()-

PHP:atan()的用法_Math函数
atan(PHP4,PHP5)atan反正切说明floatatan(float$arg)返回arg的反正切值,单位是弧度。atan()是tan()的反函数,它的意思是在atan()范围里的每个值都是a==tan(atan(a))。参

本周排行

更新排行

强悍的草根IT技术社区,这里应该有您想要的!
Copyright © 2010 Gimoo.Net. All Rights Rreserved  京ICP备05050695号