提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
给定一个数字格式的ip,返回一个字节数组,该数组可以被馈送到InetAddress。
//package com.nowjava; /** 来 自 时代Java公众号**/ public class Main { public static void main(String[] argv) throws Exception { long ip = 2; System.out.println(java.util.Arrays .toString(numericIpToByteArray(ip))); } /** * Given an ip in numeric format, return a byte array that can be fed to * InetAddress. * @param ip the ip address in long (such as 3232235780) * @return the byte array. */ public static byte[] numericIpToByteArray(long ip) { byte[] ipArray = new byte[4]; ipArray[3] = (byte) (ip & 0xff); ipArray[2] = (byte) ((ip >> 8) & 0xff); ipArray[1] = (