更改 PHP 中的字节顺序

发布于 2024-12-06 23:10:07 字数 381 浏览 4 评论 0原文

因此,我正在 PHP 中创建一个类来解析 VPK 文件格式。

然而我遇到了一个问题:

object(VPKHeader)#2 (3) {
    ["Signature"]=>
  string(8) "3412aa55"
    ["Version"]=>
  string(4) "1000"
    ["DirectoryLength"]=>
  int(832512)
}

签名应该是0x55aa1234,但是我正在读取的签名是0x3412aa55。

如何切换 PHP 中的字节序?

So i'm making a class in PHP to parse the VPK file format.

However i've hit a problem:

object(VPKHeader)#2 (3) {
    ["Signature"]=>
  string(8) "3412aa55"
    ["Version"]=>
  string(4) "1000"
    ["DirectoryLength"]=>
  int(832512)
}

The signature is supposed to be 0x55aa1234, however the signature I'm reading is 0x3412aa55.

How do I switch the endianness in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2024-12-13 23:10:07

如果您的十六进制值始终是字符串,您可以使用以下函数:

function swapEndianness($hex) {
    return implode('', array_reverse(str_split($hex, 2)));
}

同意,它不是最有效的,但在我看来代码非常优雅。此外,它适用于所有大小的数字。

If your hex values will always be strings, you can use the following function :

function swapEndianness($hex) {
    return implode('', array_reverse(str_split($hex, 2)));
}

Agreed, it's not the most efficient, but the code is quite elegant in my opinion. Also, it works with all sizes of numbers.

成熟稳重的好男人 2024-12-13 23:10:07

您必须手动转换该值。该算法将与 C++ 中的算法相同,因此只需移植此代码(您只需要适用于 int afaik 的代码):

inline void endian_swap(unsigned short& x)
{
    x = (x>>8) | 
        (x<<8);
}

// this is the one you need
inline void endian_swap(unsigned int& x)
{
    x = (x>>24) | 
        ((x<<8) & 0x00FF0000) |
        ((x>>8) & 0x0000FF00) |
        (x<<24);
}

// __int64 for MSVC, "long long" for gcc
inline void endian_swap(unsigned __int64& x)
{
    x = (x>>56) | 
        ((x<<40) & 0x00FF000000000000) |
        ((x<<24) & 0x0000FF0000000000) |
        ((x<<8)  & 0x000000FF00000000) |
        ((x>>8)  & 0x00000000FF000000) |
        ((x>>24) & 0x0000000000FF0000) |
        ((x>>40) & 0x000000000000FF00) |
        (x<<56);
}

来源:http://www.codeguru.com/forum/showthread.php?t=292902

You have to convert the value manually. The algorithm will be the same as it is in C++, so just port this code (you only need the one that works on int afaik):

inline void endian_swap(unsigned short& x)
{
    x = (x>>8) | 
        (x<<8);
}

// this is the one you need
inline void endian_swap(unsigned int& x)
{
    x = (x>>24) | 
        ((x<<8) & 0x00FF0000) |
        ((x>>8) & 0x0000FF00) |
        (x<<24);
}

// __int64 for MSVC, "long long" for gcc
inline void endian_swap(unsigned __int64& x)
{
    x = (x>>56) | 
        ((x<<40) & 0x00FF000000000000) |
        ((x<<24) & 0x0000FF0000000000) |
        ((x<<8)  & 0x000000FF00000000) |
        ((x>>8)  & 0x00000000FF000000) |
        ((x>>24) & 0x0000000000FF0000) |
        ((x>>40) & 0x000000000000FF00) |
        (x<<56);
}

Source: http://www.codeguru.com/forum/showthread.php?t=292902

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文