Convert IP between IPv4 and numerical format in java

后端 未结 4 1301
猫巷女王i
猫巷女王i 2021-01-05 14:46

An IPv4 can have more representations: as string (a.b.c.d) or numerical (as an unsigned int of 32 bits). (Maybe other, but I will ignore them.)

Is there any

4条回答
  •  鱼传尺愫
    2021-01-05 15:28

    I would suggest:

    long ip2long(){
        ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.BIG_ENDIAN);
        buffer.put(new byte[] { 0,0,0,0 });
        buffer.put(socket.getInetAddress().getAddress());
        buffer.position(0);
        return buffer.getLong();
    }
    

提交回复
热议问题