IP-addresses stored as int results in overflow?

后端 未结 11 463
無奈伤痛
無奈伤痛 2021-02-02 12:27

I\'m writing a chat-server in node.js, and I want to store connected users IP-addresses in a mysql database as (unsigned) integers. I have written a javascript method to convert

相关标签:
11条回答
  • 2021-02-02 13:05

    One-Liner:

    const ipToLong = ip => ip.split('.').map(parseFloat).reduce((total, part) => total * 256 + part);
    
    0 讨论(0)
  • 2021-02-02 13:09

    Try this solution, it might help:

    function IpToInteger(ipAddr)
    {
        var parts = ipAddr.split('.');
        return (((parts[0] ? parts[0] << 24 : 0) |
                 (parts[1] ? parts[1] << 16 : 0) |
                 (parts[2] ? parts[2] << 8  : 0) |
                 (parts[3])) >>> 0);
    }
    
    0 讨论(0)
  • 2021-02-02 13:09
    function IpAddressToLong(ip){
      return ip.split('.').map((octet, index, array) => {
          return parseInt(octet) * Math.pow(256, (array.length - index - 1));
        }).reduce((prev, curr) => {
          return prev + curr;
      });
    }
    

    Taken from repo

    0 讨论(0)
  • 2021-02-02 13:11

    I revised Evan's final answer a bit, particularly dot2num. It functions the same but might be more readable and is marginally slower.

    function ip2num(ip) {
        var parts = ip.split('.');
    
        var num = 0;
        num += d[0] * Math.pow(2, 24);
        num += d[1] * Math.pow(2, 16);
        num += d[2] * Math.pow(2, 8);
        num += d[3];
    
        return num;
    }
    
    function num2ip(num) {
        var ip = num % 256;
    
        for (var i=3; i > 0; i--) { 
            num = Math.floor(num / 256);
            ip = num % 256 + '.' + ip;
        }
    
        return ip;
    }
    
    0 讨论(0)
  • 2021-02-02 13:12

    Why is the converted IP negative?

    It's NOT an overflow. The first part of your IP address is 192 which converts to 11000000 in binary. You then shift that all the way to the left. When there is a 1 in the leftmost position of a 32 bit number, it's negative.

    How do you convert back to a string?

    Do the same thing you did to convert from a string but in reverse. Shift right (and mask)!

    function intToIP(int) {
        var part1 = int & 255;
        var part2 = ((int >> 8) & 255);
        var part3 = ((int >> 16) & 255);
        var part4 = ((int >> 24) & 255);
    
        return part4 + "." + part3 + "." + part2 + "." + part1;
    }
    

    Why reinvent the wheel? From Google:

    OR, you can use what I found here:
    http://javascript.about.com/library/blipconvert.htm

    function dot2num(dot) 
    {
        var d = dot.split('.');
        return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
    }
    
    function num2dot(num) 
    {
        var d = num%256;
        for (var i = 3; i > 0; i--) 
        { 
            num = Math.floor(num/256);
            d = num%256 + '.' + d;
        }
        return d;
    }
    
    0 讨论(0)
  • 2021-02-02 13:16
    var aaa = Number("0b"+ "192.168.2.44".split(".").map(
      function(dec){
        return ("00000000" + Number(dec).toString(2)).slice(-8); 
      }).join(""));
    
    aaa.toString(2).match(/.{1,8}/g).map(
      function(bin){
        return Number("0b"+bin); 
      }).join(".");
    
    0 讨论(0)
提交回复
热议问题