如何使用php将十进制整数转换为字节


How to convert a decimal integer to bytes with php?

我需要用php获取多少字节是十进制整数。例如,我如何知道256379是否是php的3字节?我需要一个php函数来传递256379作为输入,并获得3作为输出。我怎么能得到它?

您需要像这样计算对数:

echo ceil( log ($nmber, 256) );

表示一个数字所需的字节数可以这样计算:

echo getNumBytes(256379); // Output: 3
echo getNumBytes(25637959676); // Output 5
function getNumBytes($num) {
    $i = 1;
    do {
        $i++;
    } while(pow(256,$i) < $num);
    return $i;
}