8

I was wondering if there is a easy way to display the 8 bits of a byte(or char) in PHP.

For example for ASCII encoding the character '0' should return 0011 0000

Thanks for your input!

4

3 回答 3

16

这应该做的工作:

$bin = decbin(ord($char));
$bin = str_pad($bin, 8, 0, STR_PAD_LEFT);
于 2013-05-26T19:40:43.230 回答
4

另一种解决方案,这包括 4 位数字之间的空格:

$char = 0;
echo chunk_split(sprintf('%08b', ord($char)), 4, ' ');
于 2013-05-26T19:55:01.637 回答
4

您可以为此使用按位运算符

$a='C';
for ($i=0; $i<8; $i++) {
  var_dump((ord($a) & (1<<$i))>>$i);
}

输出:

int(1)
int(1)
int(0)
int(0)
int(0)
int(0)
int(1)
int(0)
于 2013-05-26T19:43:38.627 回答