设为首页收藏本站淘宝杂货铺

从F到0 - From F to 0

 找回密码
 注册已关闭
搜索
查看: 2178|回复: 3
收起左侧

[PHP/ASP/JSP] PHP反转Short(短整数)/int(整数)/Long(长整数)字节序/大小端转换/Char半字节交换

[复制链接]
发表于 2019-3-14 10:51:18 | 显示全部楼层 |阅读模式


  1. <?php
  2. function Swap($char){   //半字节交换
  3. return ($char&15)<<4|($char>>4);
  4. }
  5. function ReverseShortBytes($short){ //反转短整数字节序(16位)
  6. return ($short&255)<<8|($short>>8);
  7. }
  8. function ReverseIntBytes($int){  //反转整数字节序(32位)
  9. return ReverseShortBytes($int & 0xFFFF) << 16 | ReverseShortBytes($int >> 16);
  10. }
  11. function ReverseLongBytes($long){ //反转长整数字节序(64位PHP专用)
  12. return ReverseIntBytes($long & 0xFFFFFFFF) << 32 | ReverseIntBytes($long >> 32);
  13. }
  14. echo dechex(ReverseLongBytes(0x0123456789ABCDEF))."<br>";
  15. echo dechex(ReverseIntBytes(0xABCD1234))."<br>";
  16. echo dechex(ReverseShortBytes(0xABCD))."<br>";
  17. echo dechex(Swap(0x7F))."<br>";
  18. ?>

复制代码

发表于 2019-3-14 18:11:31 | 显示全部楼层
不错顶顶顶顶顶顶
您需要登录后才可以回帖 登录 | 注册已关闭

本版积分规则

QQ|手机版|Archiver|从F到0 ( 蒙ICP备17002595号-1 )
蒙公网安备15010402000325号

腾讯云安全认证

GMT+8, 2024-4-20 05:08 , Processed in 0.451025 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表