1

I have a method which converts an IPv6 address to its decimal value.

At the moment my result is:

4.254076641128259e+37

But I need this:

42540766411282592857539836924043198464

My code:

var ip = '2001:0db8:0:0:8d3:0:0:0';
var address = new v6.Address(ip);
var bin = address.binaryZeroPad();
var dec = 0;
for (var i = 0; i < bin.length; i++) {
    dec = dec + parseInt(bin[i]) * Math.pow(2, bin.length - 1 - i);
}
console.log(dec);

Im using the ip-address lib to convert IPv6 first to binary.

Jibi Abraham
  • 4,636
  • 2
  • 31
  • 60
mk2015
  • 205
  • 2
  • 14
  • 3
    possible duplicate of [How to avoid scientific notation for large numbers in JavaScript?](http://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript) – Halfpint Jul 16 '15 at 11:42
  • You cannot represent an unsigned 128 bit number as a JavaScript number. JavaScript Number only allows storage of integers up to 2^53. See this answer. http://stackoverflow.com/a/307200/898289. I'm just working on alternative solution.... – Adam Jul 16 '15 at 11:57
  • Also note, this is not a duplicate, the answer linked does not deal with large numbers. – Adam Jul 16 '15 at 11:58
  • is it maybe possible to add each section to a string? – mk2015 Jul 16 '15 at 12:11
  • You could use [BigInteger](https://github.com/peterolson/BigInteger.js) – Bali Balo Jul 16 '15 at 12:12
  • @ Bali Balo BigInteger.js readme says: Note that Javascript numbers larger than 9007199254740992 and smaller than -9007199254740992 are not precisely represented numbers and will not produce exact results. If you are dealing with numbers outside that range, it is better to pass in strings. – mk2015 Jul 16 '15 at 12:21
  • No way, values in javascript has only 52 bits of precision. The only solution is a bignum method – phuclv Jul 16 '15 at 12:42

1 Answers1

2

You cannot represent an unsigned 128 bit number as a JavaScript Number without losing precision. JavaScript Number only allows storage of integers up to 2^53. See What is JavaScript's highest integer value that a Number can go to without losing precision?

See the 08d3 group has been lost...

var dec = 42540766411282592857539836924043198464;
console.log(dec.toString(16))
// ==> 20010db8000000000000000000000000

An alternative is to use another library to simulate arbitrary large integers, like BigInteger (WTFPL license).

Example with corresponding fiddle

var ip = '2001:0db8:0:0:8d3:0:0:0';

// simulate your address.binaryZeroPad(); method
var parts = [];
ip.split(":").forEach(function(it) {
    var bin = parseInt(it, 16).toString(2);
    while (bin.length < 16) {
        bin = "0" + bin;
    }
    parts.push(bin);
})
var bin = parts.join("");

// Use BigInteger library
var dec = bigInt(bin, 2).toString();
console.log(dec);

Output

42540766411282592857539836924043198464
ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Adam
  • 35,919
  • 9
  • 100
  • 137
  • when i try to convert this i get a wrong value: 2a01:0ad0:0022::0 – mk2015 Jul 16 '15 at 13:15
  • My IP6 conversion does not deal with :: notation. I only included it because I did not have access to whatever code provides new v6.Address(ip).... Out of interest what library is that? I couldn't find it on google.... – Adam Jul 16 '15 at 13:19