IP-addresses stored as int results in overflow?

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

    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);
    }
    

提交回复
热议问题