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

      

  • 相关阅读:
    POJ 2337 【欧拉路径<包含输出>】.cpp
    Hlg 【表达式求值+欧拉路径】.cpp
    Hlg 1563 亲合数.cpp memset
    Hlg 1619 只有矩形.cpp【并查集】
    用unison来同步你的远程文件夹 Fwolf's Blog
    调试intellij IDEA hbase开发环境
    Bash: parsing arguments with ‘getopts’ | rsalveti's random thoughts
    十天内提高单词量到20000! (Vocabulary 10000)
    Unison File Synchronizer User Manual and Reference Guide
    Import errors in djangocms
  • 原文地址:https://www.cnblogs.com/kaleidoscope/p/9832470.html
Copyright © 2020-2023  润新知