0)) { $int = 0xFFFF8000 | $int; } // Convert the value return (int) self::dword2signedInt($int); } /** * bytes2unsignedInt * * The function converts array of 2 or 4 bytes to unsigned integer. * The return value depends on order of the input bytes (endianning). * * @param array $values * @param bool $endianness * @return int|float */ public static function bytes2unsignedInt($values, $endianness = 0) { $data = array(); $int = 0; // Set the array to correct form $data = self::checkData($values); // Combine bytes $int = self::combineBytes($data, $endianness); // Convert the value return self::dword2unsignedInt($int); } /** * bytes2string * * The function converts an values array to the string. The function detects * the end of the string by 0x00 character as defined by string standards. * * @param array $values * @param bool $endianness * @return string */ public static function bytes2string($values, $endianness = 0) { // Prepare string variable $str = ""; // Parse the received data word array for($i=0;$i4 || count($data)==3) { throw new Exception('The input data should be an array of 2 or 4 bytes.'); } // Fill the rest of array by zeroes if (count($data) == 2) { $data[2] = 0; $data[3] = 0; } // Check the values to be number if (!is_numeric($data[0]) || !is_numeric($data[1]) || !is_numeric($data[2]) || !is_numeric($data[3])) { throw new Exception('Data are not numeric or the array keys are not indexed by 0,1,2 and 3'); } return $data; } /** * combineBytes * * Combine bytes together * * @param int $data * @param bool $endianness * @return int */ private static function combineBytes($data, $endianness) { $value = 0; // Combine bytes if ($endianness == 0) $value = (($data[3] & 0xFF)<<16) | (($data[2] & 0xFF)<<24) | (($data[1] & 0xFF)) | (($data[0] & 0xFF)<<8); else $value = (($data[3] & 0xFF)<<24) | (($data[2] & 0xFF)<<16) | (($data[1] & 0xFF)<<8) | (($data[0] & 0xFF)); return $value; } } ?>