IP-addresses stored as int results in overflow?

后端 未结 11 470
無奈伤痛
無奈伤痛 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: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

提交回复
热议问题