zoukankan      html  css  js  c++  java
  • Why is long2ip conversion important?

    Frequently Asked Questions about Convert long to IP address https://long2ip.com/faq/

    Where is long2ip used?

    Long2ip conversion is mainly used to swiftly look up an IP address without having to make time-consuming calculations to get there. IP addresses are often saved as long in databases because the server can perform queries more quickly, but this also means that they basically become almost unreadable for the human eye.

    Why is long2ip conversion important?

    IP addresses are often saved in databases as decimals, because it's easier for a database server to compare numbers with each other than to compare IP addresses. Because it's very difficult for people to be able to see or even imagine a long IP, we use long2ip converter tools to quickly look up an IP address.

    What is long2ip conversion?

    A long integer is a way of saving an IP address as a number in a database. A number is easier to use than an IP address in databases when making comparisons. 

    An IP address consists of 4 numbers that are divided by points, for example 8.8.8.8. 

    An IP address is "base 256", and to convert the IP address 8.8.8.8.to decimal (base 10) we use the following formula: 
    8 x (256)^3 + 8 x (256)^2 + 8 x (256)^1 + 8 (256)^0

    package com.test.long2ip;
    
    public class Long2ipConversion {
        public Long getPow(int i) {
            return (long) Math.pow(256, i);
        }
    
        public String long2ip(long long_) {
            long i = long_;
            short part3 = (short) (long_ % getPow(1));
            i -= part3;
            short part2 = (short) (i % getPow(2) / getPow(1));
            i -= part2 * getPow(1);
            short part1 = (short) (i % getPow(3) / getPow(2));
            i -= part1 * getPow(2);
            short part0 = (short) (i / getPow(3));
            return part0 + "." + part1 + "." + part2 + "." + part3;
        }
    
        public String long2ip(String long_) {
            long intPointIp = Long.parseLong(long_.trim());
            return long2ip(intPointIp);
        }
    
        public long ip2long(String ip_) {
            String[] ip_s = ip_.trim().split("\.");
            long res = 0;
            short ii = 3;
            for (String i : ip_s) {
                res += Long.parseLong(i) * getPow(ii);
                ii -= 1;
            }
            return res;
        }
    
    }

    https://github.com/toywei/long2ip



     
  • 相关阅读:
    Java 实例
    为什么很多程序员工作时都戴耳机?
    HTTP状态码大全
    Eclipse怎么切换工作空间
    maven POM.xml内的标签大全详解
    利用html5的FormData对象实现多图上传
    后台定时器注解方式
    js多定时器
    解决ios上微信无法捕获返回键按钮事件的问题
    上传文件,获取表单数据和文件流
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10180332.html
Copyright © 2011-2022 走看看