dpbfb7119 2011-06-02 22:02
浏览 12
已采纳

在PHP中交换文本的字节

I basically need to port this piece of code to php

for (i = 0; i < 128/4; i++)
    data32[i] = bswap_32(data32[i]);

But, there is no bswap function in php.

Would someone be kind enough to provide me with something that could solve the problem?

  • 写回答

2条回答 默认 最新

  • dsyct08008 2011-06-02 22:08
    关注

    This should do it (untested):

    function bswap_32($j)
    {
        return (($j & 255) << 24) | (($j & 0xff00) << 8) |
               (($j & 0xff0000) >> 8) | (($j & 0xff000000) >> 24);
    }
    

    Or, if there is a sign extension problem, this should resolve it:

    function bswap_32($j)
    {
        return (($j & 255) << 24) | (($j & 0xff00) << 8) |
               (($j & 0xff0000) >> 8) | (255 & (($j & 0xff000000) >> 24));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?