Print decimal to binary but result be 16 bit number

echo sprintf( "%016d", decbin(16384)); // how this I think is correct===> 0100000000000000 but I get 0000002147483647

I want print decimal to binary but result be 16 bit number if eg result decbin()=10 digits binary in front of it have 6 zeros (0) total 16 bits…

is the above correct syntax…? I get decimal - what error is?

Try this:

for($i=0; $i<10; $i++):
    $a = sprintf( "decimal: % 2d  ==>  binary: %016d", $i, $i); 
    echo '<br />', $a;

**Output:** [quote]

decimal: 0 ==> binary: 0000000000000000
decimal: 1 ==> binary: 0000000000000001
decimal: 2 ==> binary: 0000000000000002
decimal: 3 ==> binary: 0000000000000003
decimal: 4 ==> binary: 0000000000000004
decimal: 5 ==> binary: 0000000000000005
decimal: 6 ==> binary: 0000000000000006
decimal: 7 ==> binary: 0000000000000007
decimal: 8 ==> binary: 0000000000000008
decimal: 9 ==> binary: 0000000000000009

Would you not use

$a = sprintf("Decimal: %d, Binary: %016b", $i, $i);

The PHP doc seems to suggest this would work.

Failing that, just use

$x = decbin(16384);
echo str_pad($x, 16, "0", STR_PAD_LEFT);

which you could obviously convert to a function.

Remember that decbin() returns a string, so you can’t just stick that into printf() as if it’s a number.

Binary meaning 1 and 0

I try and last solution you refer…

0100000000000000 this get in mac I think works

Only in windows xampp seem exist

I find a second way … get a str 16 zeros and replace from 16-strlen (binaryStr) to end of with binaryStr with sub_replace

You know why in windows the first problem…?

I can’t see how the first code you posted would give the expected result on any platform. The format string “%016d” says to get the input number, convert it to decimal, and pad it with up to sixteen zeroes and display it. But the output from decbin() is a string, not a number, so you can’t stick it into a decimal output format.

I tested this code:

$a = sprintf("%016b", 16384);
echo $a;

and I get


The first user-contributed note on the PHP manual page for decbin() suggests the method you posted in the beginning, but I don’t see how it can work.

OK I will check

if (! is_int($number)) {

I want false if
“0”, “32”
but true if
“f”, “H7484”, “KBD”

how do this?

I get true in case “0”

if (!$number==“0” && ( (int)$number==null || !is_int((int)$number))) { // This works in ALL cases???

I’m not entirely clear on the question, however keep in mind that is_int() won’t work with strings (i.e. form input) - you need to use is_numeric() for that. However it does return valid for values that can be numbers, for example if your user enters Octal or Binary numbers as strings and, in earlier versions, hexadecimal as well.

You could write a quick function to test that all characters in a string are either digits or decimal points (and maybe commas) to do exactly what you want, if there isn’t something there already.

Solved and this with is_numeric

Thanks for help in the matter

I have build an assembler with php of Hack Computer for a course in coursera From NAND to Tetris…
In case I want to compile an assembler program over 20000 lines to machine lang that’s zeros and ones this should take time… after run it how do browser not stop responding like do normally in 30sec , if this run takes 3-6min…? Basically do browser await like a download doing…

Maybe you could do some kind of javascript that would check every ten seconds or so. That way your browser wouldn’t be waiting with a risk of time-out.

What about
Exist for timeout and setting in xampp eg php.ini to set for 10min… in case like a download…?

If it’s for your use only you can tweak the memory and timeout settings on your own localhost server as you like,

I don’t know as I’d go straight to ten minutes though, I’d inch my way up.

As a general rule, needing to throw more memory and time at a script is a strong suggestion that the script needs to be better written. Though in this case I think you are probably only pushing the limits of PHP to do something it isn’t primarily meant to do, This is where others would start looking at other languages to do the work.

But anyway, as I said, if it’s only for you on your own machine, abuse it as much as you can get away with.

Can run php script CLI. …?

On your computer? I don’t see why not.

But I have the feeling that if you have to ask, it might be more complex than how you’re used to doing things.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.