Skip to content

Instantly share code, notes, and snippets.

@sepehr
Created August 1, 2017 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sepehr/76fed29f24d88e4665bdb978a766c525 to your computer and use it in GitHub Desktop.
Save sepehr/76fed29f24d88e4665bdb978a766c525 to your computer and use it in GitHub Desktop.
Change endianness of a decimal number in PHP
<?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