note 36568 added to function.decbin

From: Date: Wed, 15 Oct 2003 06:38:40 +0000
Subject: note 36568 added to function.decbin
Groups: php.notes 
Request: Send a blank email to php-notes+get-58605@lists.php.net to get a copy of this message
This is just an extension off of the first comment. This is a pair of function to convert from an array of binary values to an integer and vice versa. It has a touch of error checking. /** * Will convert an array of binary values into an integer for storage * * @param array $data_array Array of 31 or less binary values * @return integer Encoded integer */ function array_to_binary_int($data_array) { if (count($data_array) > 31) return FALSE; foreach ($data_array as $key => $value) { if ($value) $data_array[$key] = 1; if (!$value) $data_array[$key] = 0; } $binstring = strrev(implode('', $data_array)); $bit_integer = bindec($binstring); return $bit_integer; } /** * Will convert a stored integer into an array of binary values * * @param integer $data_integer Encoded integer * @return integer Array of binary values */ function binary_int_to_array($data_integer) { if (($data_integer > 2147483647) OR ($data_integer < 0)) return FALSE; $binstring = strrev(str_pad(decbin ($data_integer),31,"0",STR_PAD_LEFT)); $bitarray = explode(":",chunk_split($binstring, 1, ":")); return $bitarray; } ---- Manual Page -- http://www.php.net/manual/en/function.decbin.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36568 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36568&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36568&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#58605) next »