简体   繁体   English

如何在PHP中将从十进制转换为16位二进制数的数字零填充?

[英]How do I zero-pad a number converted from decimal to 16-bit binary in PHP?

I know how to do that simply with 我知道如何做到这一点

str_pad(decbin($number),16,"0",STR_PAD_LEFT)

However, it appears that using 但是,似乎使用

printf("blah",$number)

should do this faster, but I have no clue what the "blah" formatting characters have to be. 应该可以更快地执行此操作,但是我不知道“ blah”格式字符必须是什么。 As this will have to be done millions of times, any speed-up is useful. 由于必须进行数百万次,因此任何加速都是有用的。 The only example I found used "%08b" to zero-pad to 8-bit a decimal number converted to binary. 我发现的唯一示例使用“%08b”将零位填充为8位十进制数,并将其转换为二进制数。 And no, "%016b" doesn't work. 不,“%016b”不起作用。 All other examples avoid going beyond 8 bits. 所有其他示例都避免超出8位。 The PHP.NET manual is just gibberish to me. PHP.NET手册对我来说简直是胡言乱语。

For me %016b does work: 对我来说, %016b 确实有效:

<?php
$number = decbin(11);
printf("%016b",$number);

The output I get is: 我得到的输出是:

0000001111110011

This is PHP-7.0.18 on a x86-64 Linux system. 这是x86-64 Linux系统上的PHP-7.0.18。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM