Java: convert int to InetAddress

后端 未结 10 1231
既然无缘
既然无缘 2020-12-14 06:34

I have an int which contains an IP address in network byte order, which I would like to convert to an InetAddress object. I see that there is an <

10条回答
  •  生来不讨喜
    2020-12-14 07:37

    public static byte[] int32toBytes(int hex) {
        byte[] b = new byte[4];
        b[0] = (byte) ((hex & 0xFF000000) >> 24);
        b[1] = (byte) ((hex & 0x00FF0000) >> 16);
        b[2] = (byte) ((hex & 0x0000FF00) >> 8);
        b[3] = (byte) (hex & 0x000000FF);
        return b;
    
    }
    

    you can use this function to turn int to bytes;

提交回复
热议问题