Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the endianness type in PHP?

In C# I can get the endianness type by this code snippet:

if(BitConverter.IsLittleEndian)
{
   // little-endian is used   
}
else
{
   // big-endian is used
}

How can I do the same in PHP?

like image 267
HomeCoder Avatar asked Mar 16 '12 21:03

HomeCoder


1 Answers

PHP's string type is an 8-bit binary string, a char sequence. It has no endianness. Thus for the most part endianness is a non-issue in PHP.

If you need to prepare binary data in a specific endianness, use the pack() and unpack() functions.

If you need to determine the machine's native endianness, you can use pack() and unpack() in the same way.

function isLittleEndian() {
    $testint = 0x00FF;
    $p = pack('S', $testint);
    return $testint===current(unpack('v', $p));
}
like image 179
Francis Avila Avatar answered Sep 28 '22 07:09

Francis Avila