Jump to content

Binary Number


Mencarta

Recommended Posts

I'm not sure if casting as int will work, the ord function is specifically for returning the ASCII value of a character, so returning the ASCII value of A would give 65.http://www.php.net/manual/en/function.ord.phpOnce you have the decimal ASCII value, you can use the pack function to format it as a binary string. A format of "c" would be appropriate to use for this.http://www.php.net/manual/en/function.pack.phpOther examples on that page as well.

Link to comment
Share on other sites

Note that if you want to check if the number has a certain bit set (or not), it will probably be faster to use a bitwise operator, like "|" or "&", like so:

$int = 5;//101 in binary$mark = 2;//10 in binary$check = $int & $mark;//2; 10 in binaryif ($check === $mark) {//true

If you just want to display it, decbin() it is.

Link to comment
Share on other sites

In that example $check would be 0, if it's using and on 5 and 2. If it was 6 and 2 then check would be 2.
Opps... indeed... I partly copied and reworked another example I had, and I forgot to update the comments accordingly.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...