Skip to content
Advertisement

How this function should be properly ported from PHP to Javascript

I am working on a project where I need an encoding that I can only get from a PHP project:

JavaScript

Here is my Javascript implementation:

JavaScript

The problem comes when I run a simple test, I instantiate the two binary streams and I wrote a value on them, the problem is that output is different.

Here is the PHP test & output:

JavaScript

Here is the Javascript test & output:

JavaScript

As we can see outputs are different

Complete files:

Advertisement

Answer

These methods are hitting an issue where the bitwise operators are only working in 32-bit, so the 64-bit numbers are being truncated before your serialisation.

You can work around this with BigInt.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement