如何获得在PHP中的字节顺序类型? [英] How to get the endianness type in PHP?

查看:217
本文介绍了如何获得在PHP中的字节顺序类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我可以通过这个code段获得字节序类型:

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

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

我怎么可以这样做在PHP?

How can I do the same in PHP?

推荐答案

PHP的字符串类型是一个8位二进制字符串,一个字符序列。它没有字节顺序。因此,对于大部分字节顺序是PHP的一个非问题。

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.

如果您需要prepare二进制数据在特定字节顺序,使用 包() 解压() 功能。

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));
}

这篇关于如何获得在PHP中的字节顺序类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆