一种方便的方法,它接受表示为长的IP地址,并返回表示同一IP地址的大小为4的字节数组。
/* 来自 *时 代 J a v a 公 众 号 - nowjava.com*/ /* * This file is part of VIUtils. * * Copyright ? 2012-2015 Visual Illusions Entertainment * * VIUtils is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU General Public License along with this library. * If not, see http://www.gnu.org/licenses/lgpl.html. */ import java.util.regex.Matcher; import java.util.regex.Pattern; import static net.visualillusionsent.utils.Verify.notNull; public class Main{ public static void main(String[] argv) throws Exception{ long address = 2; System.out.println(java.util.Arrays.toString(longToIPv4(address))); } /** * A convenient method that accepts an IP address represented as a long and * returns an byte array of size 4 representing the same IP address. * * @param address * the long value representing the IP address. * * @return An {@code byte[]} of size 4. */ public static byte[] longToIPv4(long address) { byte[] ip =