Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {

    public static int IPAddr2int(String IPAddr) {
        byte[] ip = new byte[4];
        int position1 = IPAddr.indexOf(".");
        int position2 = IPAddr.indexOf(".", position1 + 1);
        int position3 = IPAddr.indexOf(".", position2 + 1);
        ip[0] = Byte.parseByte(IPAddr.substring(0, position1));
        ip[1] = Byte.parseByte(IPAddr.substring(position1 + 1, position2));
        ip[2] = Byte.parseByte(IPAddr.substring(position2 + 1, position3));
        ip[3] = Byte.parseByte(IPAddr.substring(position3 + 1));
        return bytes2int(ip, 0);
    }

    public static int bytes2int(byte[] data, int start) {
        int ret;
        if (data.length - start >= 4) {
            ret = (int) (((data[start + 3] & 0xff) << 24) | ((data[start + 2] & 0xff) << 16)
                    | ((data[start + 1] & 0xff) << 8) | (data[start] & 0xff));
            return ret;
        } else
            return 0;
    }
}