IP地址转换32为长整型

Programming Question:

Convert an IPv4 address in the format of null-terminated C string into a 32-bit integer.For example, given an IP address “172.168.5.1”, the output should be a 32-bit integer with “172” as the highest order 8 bit, 168 as the second highest order 8 bit, 5 as the second lowest order 8 bit, and 1 as the lowest order 8 bit. That is,"172.168.5.1" => 2896692481


Requirements:

  • You can only iterate the string once.
  •  You should handle spaces correctly: a string with spaces between a digit and a dot is a valid input; while a string with spaces between two digits is not.
  1. "172[Space].[Space]168.5.1" is a valid input. Should process the output normally.
  2. "1[Space]72.168.5.1" is not a valid input. Should report an error.
  • Please provide unit tests.

 

public class IPV4Demo {

    private static final String INVALID_FORMAT = "invalid format";
    private static final long   INVALID_CODE   = -1;

    public long parseIP4Address(String input) {
        if (StringUtils.isBlank(input) || input.startsWith(" ") || input.startsWith(".") || input.endsWith(" ")
                || input.endsWith(".")) {
            System.out.println(INVALID_FORMAT);
            return INVALID_CODE;
        }

        long sum = 0;
        int shift = 3;
        String[] array = input.split("\\.");
        if (array.length != 4) {
            System.out.println(INVALID_FORMAT);
            return INVALID_CODE;
        }

        boolean parseError = false;
        for (int index = 0; index < array.length; index++) {
            try {
                long segment = Long.parseLong(array[index].trim());
                sum += segment << ((shift--) * 8);
            } catch (Exception ex) {
                parseError = true;
                break;
            }
        }
        if (parseError) {
            System.out.println(INVALID_FORMAT);
            return INVALID_CODE;
        }
        return sum;
    }
}

 

Unit Test

public class IPV4DemoTest {

    private static IPV4Demo demo = new IPV4Demo();  
    @Test
    public void testNull(){
        Long value  = demo.parseIP4Address(null);
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    @Test
    public void testEmptyString(){
        Long value  = demo.parseIP4Address("");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    
    @Test
    public void testEmptyString2(){
        Long value  = demo.parseIP4Address(" 172.168.5.1");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    
    @Test
    public void testEmptyString3(){
        Long value  = demo.parseIP4Address("172.168.5.1 ");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    
    @Test
    public void testRegular(){
        Long value  = demo.parseIP4Address("172.168.5.1");
        Assert.assertTrue(Objects.equal(2896692481l, value));
    }
    @Test
    public void testWithSpace_01(){
        Long value  = demo.parseIP4Address("172 .168.5.1");
        Assert.assertTrue(Objects.equal(2896692481l, value));
    }
    @Test
    public void testWithSpace_02(){
        Long value  = demo.parseIP4Address("172. 168.5.1");
        Assert.assertTrue(Objects.equal(2896692481l, value));
    }
    @Test
    public void testWithSpace_03(){
        Long value  = demo.parseIP4Address("172 . 168.5.1");
        Assert.assertTrue(Objects.equal(2896692481l, value));
    }
    @Test
    public void testWithSpace_04(){
        Long value  = demo.parseIP4Address("1 72.168.5.1");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    @Test
    public void testWithDot_01(){
        Long value  = demo.parseIP4Address("172.168.5.1.");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    @Test
    public void testWithDot_02(){
        Long value  = demo.parseIP4Address("172.168.51.");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    @Test
    public void testWithDot_03(){
        Long value  = demo.parseIP4Address("172.168.51");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    @Test
    public void testWithDot_04(){
        Long value  = demo.parseIP4Address(".172.168.51");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
    @Test
    public void testWithDot_05(){
        Long value  = demo.parseIP4Address("..168.51");
        Assert.assertTrue(Objects.equal(-1l, value));
    }
}

  

posted @ 2018-10-22 20:15  kaleidoscopic  阅读(1691)  评论(0编辑  收藏  举报