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));
    }
}

  

转载于:https://www.cnblogs.com/kaleidoscope/p/9832470.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值