1

我想使用 base_convert 函数将 60 位二进制数据转换为 Hex 代码,如下所示。但结果不正确。下面代码的输出是 4e08556312ffc00,但正确的输出是 4E08556312FFBFF。谁能告诉我为什么?60位对功能来说太大了吗?

echo "The beacon ID in Hexadecimal is".base_convert
         ("010011100000100001010101011000110001001011111111101111111111",2, 16);

感谢

4

4 回答 4

2

我在此处对另一个问题的回答中发布了一个没有此类限制的基本转换函数的实现。

于 2012-06-22T09:49:09.837 回答
0

根据base_convert浮点数文档,base_convert在大数上会丢失精度。

取而代之的是,您可以使用bin2hex,它不受限制(此函数使用并返回字符串)

于 2012-06-22T09:15:03.137 回答
0

我的测试显示超过 43 位的精度损失。

我使用默认设置在 Win XP base + easyphp 3.1.81 上工作。

我将此 base_convert 用于家谱应用程序,而 43 代的限制 - 有时 - 还不够。

于 2013-07-08T10:59:12.387 回答
0

It is the larger of:

  1. PHP_INT_MAX and
  2. The maximum of the contiguous set of integers that can be expressed as a float.

Typical PHP implementations use IEEE 754 for implementing floating point numbers, which has a 53-bit significand, so the typical limit for 32-bit environments is 2⁵³ (0x20000000000000) and for 64-bit environments, the limit is 2⁶³-1 (0x7fffffffffffffff).

There’s a trivial solution if you have the PHP gmp extension ( http://php.net/gmp ):

$hexNumber = gmp_strval( gmp_init( $binNumber, 2 ), 16 ) ;

If you have the bc extension (http://php.net/bc ), my (free & open source) WeirdoCustomDigits module will do conversions with arbitrary bases and arbitrary digit characters, without limitation.

If you're simply converting between base 2 and base 16, as with your example, you can get by without bc or gmp. See the source in the WeirdoCustomDigits module for WeirdoCustomDigits::binFromHex() and WeirdoCustomDigits::hexFromBin().

于 2014-03-12T06:26:03.957 回答