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

How to convert a decimal integer to bytes with php?

我需要用 php 得到一个十进制整数有多少字节。 例如,我怎么知道 256,379 是否是 3 字节 php? 我需要一个 php 函数来传递 256,379 作为输入并获得 3 作为输出。怎样才能拥有?

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

echo getNumBytes(256379); // Output: 3
echo getNumBytes(25637959676); // Output 5

function getNumBytes($num) {
    $i = 1;
    do {
        $i++;
    } while(pow(256,$i) < $num);
    return $i;
}

你需要像这样计算对数:

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