node-int64
Support for representing 64-bit integers in JavaScript
JavaScript Numbers are represented as IEEE 754 double-precision floats. Unfortunately, this means they lose integer precision for values beyond +/- 2^^53. For projects that need to accurately handle 64-bit ints, such as node-thrift, a performant, Number-like class is needed. Int64 is that class.
Int64 instances look and feel much like JS-native Numbers. By way of example ...
// First, let's illustrate the problem ...> 0x123456789'123456789' // <- what we expect.> 0x123456789abcdef0'123456789abcdf00' // <- Ugh! JS doesn't do big ints. :( // So let's create a couple Int64s using the above values ... // Require, of course> Int64 = // x's value is what we expect (the decimal value of 0x123456789)> x = 0x123456789Int64 value:4886718345 octets:00 00 00 01 23 45 67 89 // y's value is Infinity because it's outside the range of integer// precision. But that's okay - it's still useful because it's internal// representation (octets) is what we passed in> y = '123456789abcdef0'Int64 value:Infinity octets:12 34 56 78 9a bc de f0 // Let's do some math. Int64's behave like Numbers. (Sorry, Int64 isn't// for doing 64-bit integer arithmetic (yet) - it's just for carrying// around int64 values> x + 14886718346> y + 1Infinity // Int64 string operations ...> 'value: ' + x'value: 4886718345'> 'value: ' + y'value: Infinity'> x'100100011010001010110011110001001'> y'Infinity' // Use JS's isFinite() method to see if the Int64 value is in the// integer-precise range of JS values> true> false // Get an octet string representation. (Yay, y is what we put in!)> x'0000000123456789'> y'123456789abcdef0' // Finally, some other ways to create Int64s ... // Pass hi/lo words> 0x12345678 0x9abcdef0Int64 value:Infinity octets:12 34 56 78 9a bc de f0 // Pass a Buffer> 0x12 0x34 0x56 0x78 0x9a 0xbc 0xde 0xf0Int64 value:Infinity octets:12 34 56 78 9a bc de f0 // Pass a Buffer and offset> 00000x12 0x34 0x56 0x78 0x9a 0xbc 0xde 0xf0 4Int64 value:Infinity octets:12 34 56 78 9a bc de f0 // Pull out into a buffer> 0x12 0x34 0x56 0x78 0x9a 0xbc 0xde 0xf0<Buffer 12 34 56 78 9a bc de f0> // Or copy into an existing one (at an offset)> var buf = 1024;> 0x12 0x34 0x56 0x78 0x9a 0xbc 0xde 0xf0;