嗨,我有一根绳子193390663
我想将其转换为hex
with 2's compliment
。输出是0B86E847
现在我正在使用下面的功能,但它给了我313933333930363633
public static function String2Hex($string)
{
$hex = '';
for($i=0; $i<strlen($string); $i++)
{
$hex.=dechex(ord($string[$i]));
}
}
Update 1
试过这个
$sub2 = substr($m->msn,4,9);
$m->m_hex = dechex ($sub2);
Output
b86e847
但我想要输出像0B86E847
任何帮助将不胜感激。
您正在寻找的解决方案如下,
它是从以下给出的答案之一引用的在 PHP 中创建有符号 int 的十六进制表示 https://stackoverflow.com/questions/44135572/create-hex-representation-of-signed-int-in-php.
<?php
function signed2hex($value, $reverseEndianness = true)
{
$packed = pack('i', $value);
$hex='';
for ($i=0; $i < 4; $i++){
$hex .= strtoupper( str_pad( dechex(ord($packed[$i])) , 2, '0', STR_PAD_LEFT) );
}
$tmp = str_split($hex, 2);
$out = implode('', ($reverseEndianness ? array_reverse($tmp) : $tmp));
return $out;
}
echo signed2hex(193390663);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)