如何在PHP中将十六进制值转换为有符号整数? [英] How to convert a hexadecimal value into a signed integer in PHP?
本文介绍了如何在PHP中将十六进制值转换为有符号整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以用hexdec()方便地将它转换为无符号值,但这不给出一个有符号值。
编辑:
VB中的代码 - 两个AA十六进制值是有代表性的。
Dim bs(2)As Byte
bs(1)=AA
bs 2)=AA
Dim s As Short
s = BitConverter.ToInt16(bs,1)
解决方案
看看这个 comment 通过php.net:
hexdec()返回无符号整数。例如,hexdec(FFFFFFFE)返回4294967294,而不是-2。要转换为有符号的32位整数,您可以这样做:
<?php
echo reset(unpack(l,pack(l,hexdec(FFFFFFFE))));
?>
I am trying to convert a hex string into a signed integer.
I am able to easily transfer it into an unsigned value with hexdec() but this does not give a signed value.
Edit:
code in VB - the two "AA" hex values are representative.
Dim bs(2) As Byte
bs(1) = "AA"
bs(2) = "AA"
Dim s As Short
s = BitConverter.ToInt16(bs, 1)
解决方案
Check out this comment via php.net:
hexdec() returns unsigned integers. For example hexdec("FFFFFFFE") returns 4294967294, not -2. To convert to signed 32-bit integer you may do:
<?php
echo reset(unpack("l", pack("l", hexdec("FFFFFFFE"))));
?>
这篇关于如何在PHP中将十六进制值转换为有符号整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文