Created
August 1, 2017 07:08
-
-
Save sepehr/76fed29f24d88e4665bdb978a766c525 to your computer and use it in GitHub Desktop.
Change endianness of a decimal number in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Change endianness of the given decimal number. | |
* | |
* @param int $num | |
* | |
* @return int | |
*/ | |
function num_endianness($num) { | |
$hex = dechex($num); | |
if (strlen($hex) <= 2) { | |
return $num; | |
} | |
return hexdec(unpack('H*', strrev(pack('H*', $hex)))[1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment