"; var_dump($var); //echo ""; // 'Content-Type: application/json'); // echo json_encode($var, JSON_INVALID_UTF8_IGNORE | JSON_PRETTY_PRINT); die(); } function dump($var) { //echo "
"; var_dump($var); //echo ""; //'Content-Type: application/json'); //echo json_encode($var, JSON_INVALID_UTF8_IGNORE | JSON_PRETTY_PRINT); } function strtobits(string $str): string { $ret = ""; for ($i = 0; $i < strlen($str); ++$i) { $ord = ord($str[$i]); for ($bitnum = 7; $bitnum >= 0; --$bitnum) { if ($ord & (1 << $bitnum)) { $ret .= "1"; } else { $ret .= "0"; } } } return $ret; } function swapEndianness(string $hex) { return implode('', array_reverse(str_split($hex, 2))); } function setBit(int $n, int $p, bool $b){ return ($b ? ($n | (1 << $p)) : ($n & ~(1 << $p)) ); } function getBit(int $b, int $p){ return intval(($b & (1 << $p)) !== 0); } /** * Calculate D2S Checksum * @param $data * @return string */ function checksum($data) : string { $nSignature = 0; foreach($data as $byte){ $nSignature = ((($nSignature << 1) | ($nSignature >> 31)) + $byte) & 0xFFFFFFFF; } return swapEndianness(dechex($nSignature)); } /** * Find the position of the Xth occurrence of a substring in a string * @param $haystack * @param $needle * @param $number integer > 0 * @return int */ function strposX($haystack, $needle, $number) { if ($number == 1) { return strpos($haystack, $needle); } elseif ($number > 1) { return strpos($haystack, $needle, strposX($haystack, $needle, $number - 1) + strlen($needle)); } else { return error_log('Error: Value for parameter $number is out of range'); } } function isBit(string $bit): int { return ((int) $bit ? 1 : 0 ); } function toBits($input): string { $output = ''; if (is_string($input)){ foreach(str_split($input) as $i){ $output .= str_pad(decbin(ord($i)), 8, 0, STR_PAD_LEFT); } return $output; } else if (is_int($input)){ return str_pad(decbin($input), 8, 0, STR_PAD_LEFT); } else if (is_array($input)){ foreach ($input as $i) { $output .= tobits($i); } return $output; } } function print_mem() { /* Currently used memory */ $mem_usage = memory_get_usage(); /* Peak memory usage */ $mem_peak = memory_get_peak_usage(); echo 'The script is now using: ' . round($mem_usage / 1024) . 'KB of memory.