【问题标题】:How to convert hex to int32?如何将十六进制转换为 int32?
【发布时间】:2020-08-26 19:35:43
【问题描述】:

我正在尝试将十六进制代码从 010 Editor 转换为 int。 010 Editor

如何通过转换字符串 16100000 得到 4118? 我尝试了 hexdec 函数,但它给出了错误的结果。

echo hexdec('1610'); //gives 5648

echo hexdec('16100000'); //gives 370147328

以及如何将 4118 转换回 16100000?

【问题讨论】:

  • 那里的十六进制表示称为“小端”。另见:unpack/pack,也许是hex2bin 等。
  • echo hexdec(bin2hex(implode(array_reverse(str_split(hex2bin('16100000')))))); 更多解决方案:stackoverflow.com/questions/40828024/…
  • 马里奥,费利佩·杜阿尔特非常感谢!

标签: php laravel


【解决方案1】:

您提到的 010 编辑器中的十六进制到 int32 转换几乎没有字节序 (https://en.wikipedia.org/wiki/Endianness)。

所以,你必须以相反的顺序组装你原来的十六进制字符串。

echo hexdec('00001016');  // Would result in the 4118 you were expecting.

要恢复它,您可以使用 base_convert,从基数 10 到基数 16 并重新排列字符串:

echo base_convert('00004118', 10, 16);

哪个会给你'1016'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多