Java Text Parse textToNumericFormatV4(String ipString)

Here you can find the source of textToNumericFormatV4(String ipString)

Description

text To Numeric Format V

License

Apache License

Declaration

private static byte[] textToNumericFormatV4(String ipString) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final int IPV4_PART_COUNT = 4;

    private static byte[] textToNumericFormatV4(String ipString) {
        String[] address = ipString.split("\\.", IPV4_PART_COUNT + 1);
        if (address.length != IPV4_PART_COUNT) {
            return null;
        }/*from  w ww .  jav  a  2  s . com*/

        byte[] bytes = new byte[IPV4_PART_COUNT];
        try {
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = parseOctet(address[i]);
            }
        } catch (NumberFormatException ex) {
            return null;
        }

        return bytes;
    }

    private static byte parseOctet(String ipPart) {
        int octet = Integer.parseInt(ipPart);
        if (octet > 255 || (ipPart.startsWith("0") && ipPart.length() > 1)) {
            throw new NumberFormatException();
        }
        return (byte) octet;
    }
}

Related

  1. textToInteger(String filename, String text, String name)
  2. textToInteger(String text)
  3. textToMMDDYYYY(String pdata)
  4. textToNumericFormat(String src)
  5. textToNumericFormatV4(String host)
  6. textToNumericFormatV4(String src)
  7. textToNumericFormatV6(String src)
  8. textToPreHtml(String text)
  9. textToRLiteral(String value)