集册 Java实例教程 获取主机地址的字节数组

获取主机地址的字节数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

473
获取主机地址的字节数组


//package com.nowjava;


public class Main {//nowjava.com 提供

    public static void main(String[] argv) throws Exception {

        String hostAddress = "nowjava.com";

        System.out.println(java.util.Arrays

                .toString(getBytesOfHostAddress(hostAddress)));

    }


    /**

     * 

     * @param hostAddress

     *            ex:192.168.1.222

     * @return

     */

    public static byte[] getBytesOfHostAddress(String hostAddress) {

        String[] hostAddressParts = hostAddress.split("\\.");
/**来 自 时 代 J a v a 公 众 号 - nowjava.com**/

        byte[] bytes = new byte[hostAddressParts.length];

        for (int i = 0; i < hostAddressParts.length; i++) {

            bytes[i] = (byte) Short.parseShort(hostAddressParts[i]);

        }


        final int halfBytesLength = bytes.length / 2;

        for (int i = 0; i < halfByte
展开阅读全文