0

I use a database that stores data in decimal. I convert it to binary so I can just read it as on and off. I haven't had any issues up until the decimal length gets over 6 characters.

The following works:

$value = 2147614720;
$value = decbin($value);

Output: 10000000000000100000000000000000

However, if I try to pull that value from the database, it doesn't work if it's over 6 characters.

$value = $row['decimalvalue'];
$value = decbin($value);

Output: 1111111111111111111111111111111

Any help would be great. Thank you.

  • you are getting max integer binary value echo `decbin(2147483647);` check this https://stackoverflow.com/questions/10255724/incorrect-integer-2147483647-is-inserted-into-mysql. if you have very big integers you can use `BIGINT` – Madhawa Priyashantha Feb 24 '19 at 04:08
  • Firstly, thanks for the quick response. I'm not sure that source is helping me get the answer I am looking for. If I change the field type to varchar(20) for instance, it doesn't change anything; same result. If I print out $row['decimalvalue'] all by itself, it shows the correct value as 2147614720. It's only giving me this issue when I try to pull the data from the table and then put it into decbin(). If I do either side separately, it works. – Austin Anderson Feb 24 '19 at 05:28
  • are sure ?see this https://rextester.com/PBMBJY55859 if value is correct then decbin gives the correct binary value `1111111111111111111111111111111` is the binary value of decimal `2147483647` which means you have passed `2147483647` to decbin – Madhawa Priyashantha Feb 24 '19 at 05:38

1 Answers1

0

Until very recently, PHP was built with only 32-bit integers. That can explain why the second fails. But it is puzzling why the first example worked.

2147614720 > 2^31-1, so it turned into 2^31-1 = 2147483647.

Upgrade your PHP.

Rick James
  • 135,179
  • 13
  • 127
  • 222