What's the Prefix for Binary in PHP

What's the prefix for binary in PHP?

As of PHP 5.4+ the prefix for binary number is:

0b



For ealier version, there is no such prefix. Instead, you can use 0x, for hexadecimal.

For more informations, see the Integers section of the PHP manual.

Still, if you really need to write values using binary before PHP 5.4, you can use the bindec function, that takes a string containing the binary, and returns the corresponding value.

For example, the following portion of code :

echo bindec('10011');

Will get you :

19

But note you shouldn't do that too often : calling a function to do that each time the script is executed is quite bad for performances ^^

Instead, it's really a better solution to write your values using hexadecimal, where each digit codes for 4 bits.

Binary notation for bit comparison

Binary literals don't exist in php. Binary manipulations are usually done in hex,

error & 0010 (binary)

can be replaced with

error & 0x2

Also, see this related post What's the prefix for binary in PHP?

Is there 0b or something similar to represent a binary number in Javascript

Update:

Newer versions of JavaScript -- specifically ECMAScript 6 -- have added support for binary (prefix 0b), octal (prefix 0o) and hexadecimal (prefix: 0x) numeric literals:

var bin = 0b1111;    // bin will be set to 15
var oct = 0o17; // oct will be set to 15
var oxx = 017; // oxx will be set to 15
var hex = 0xF; // hex will be set to 15
// note: bB oO xX are all valid

This feature is already available in Firefox and Chrome. It's not currently supported in IE, but apparently will be when Spartan arrives.

(Thanks to Semicolon's comment and urish's answer for pointing this out.)

Original Answer:

No, there isn't an equivalent for binary numbers. JavaScript only supports numeric literals in decimal (no prefix), hexadecimal (prefix 0x) and octal (prefix 0) formats.

One possible alternative is to pass a binary string to the parseInt method along with the radix:

var foo = parseInt('1111', 2);    // foo will be set to 15

strrev() with binary numbers

This should work for you:

Here i use it as a string reverse it and then print the binary number

$binNumber = "0101";
echo bindec(strrev($binNumber));

Output:

10

FYI:

Since PHP +5.4 you can use the prefix 0b for binary numbers. (E.g. echo $binNumber = 0b0101; -> 5)

How to read a string in php from a binary file created from c#'s BinaryWriter

Well, my coworker wrote a method to do this for me, here it is:

function ReadVarInt($file) {
$val = 0;
$bits = 0;
while ($bits != 35) {
$n = ord(fread($file, 1));
if ($n === FALSE) return FALSE;
$val |= ($n & 0x7F) << $bits;
$bits += 7;
if (!($n & 0x80))
return $val;
}
//error "Too many bytes in what should have been a 7 bit encoded Int32.";
return FALSE;
}

function ReadString($file) {
$len = ReadVarInt($file);
if ($len === FALSE) return FALSE;
if ($len < 0) {
//error "Negative String Length";
return FALSE;
}
return fread($file, $len);
}

Simply call like this:

$myfile = fopen($filename, "r+") or die("Unable to open file!");
$string = ReadString($myfile);

PHP Binary to Hex with leading zeros

You can prepend the requisite number of leading zeroes with something such as:

$hex = str_repeat("0", floor(strspn($binary, "0") / 4)).$hex;

What does this do?

  1. It finds out how many leading zeroes your binary string has with strspn.
  2. It translates this to the number of leading zeroes you need on the hex representation. Whole groups of 4 leading zero bits need to be translated to one zero hex digit; any leftover zero bits are already encoded in the first nonzero hex digit of the output, so we use floor to cast them out.
  3. It prepends that many zeroes to the result using str_repeat.

Note that if the number of input bits is not a multiple of 4 this might result in one less zero hex digit than expected. If that is a possibility you will need to adjust accordingly.



Related Topics



Leave a reply



Submit