1

I would like to use base_convert function to convert 60 bit binary data to Hex code as follows. But the result is not correct. The output of the code below is 4e08556312ffc00 but the correct output is 4E08556312FFBFF. Can anybody tell me why? is 60 bit too large to the function?

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

Thank

user1062893
  • 89
  • 1
  • 6
  • This might be your issue: _base_convert() may lose precision on large numbers due to properties related to the internal "double" or "float" type used. Please see the [floating point numbers](http://www.php.net/manual/en/language.types.float.php) section in the manual for more specific information and limitations._ (taken from the manual) – Bono Jun 22 '12 at 09:11
  • I wonder if there is any workaround or alternative function that could convert 60 bits to hex – user1062893 Jun 22 '12 at 09:13
  • 1
    You can always do it yourself... ;) – deceze Jun 22 '12 at 09:14

4 Answers4

2

I have posted an implementation of a base conversion function without such limits in my answer to another question here.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
0

According to base_convert and floating numbers documentation, base_convert will lose precision on large numbers.

Instead of, you can use bin2hex, which is not limited (this function use and return strings)

zessx
  • 68,042
  • 28
  • 135
  • 158
  • Thank you but, bin2hex won't work here. According to the manual,the conversion is done byte-wise with the high-nibble first by the function – user1062893 Jun 22 '12 at 09:24
  • from PHP about hex2bin function and I think also for bin2hex"Caution This function does NOT convert a hexadecimal number to a binary number. This can be done using the base_convert() function." – mwafi Nov 01 '13 at 16:08
  • @mwafi That's what I wrote : `this function use and return strings`. Using strings instead of numbers, you have less limitations, but more constraints. – zessx Nov 01 '13 at 17:15
0

My tests reveal a loss of precision over 43bits.

I work on a Win XP base + easyphp 3.1.81 with default settings.

I use this base_convert for a genealogical application, and the limit of 43 generations is - sometimes - not enough.

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().

danorton
  • 11,804
  • 7
  • 44
  • 52