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



     
  • 相关阅读:
    [SDOI2009]生日礼物(单调队列)
    [luogu1638]逛画展(单调队列)
    【最短路】·SPFA算法实现
    [UVA10474]大理石在哪儿
    【转载】C++中string erase函数的使用
    【转载】高精度减法的OP写法
    【转载】ST表
    串门赛: NOIP2016模拟赛——By Marvolo 丢脸记
    Bzoj 3813 奇数国 题解 数论+线段树+状压
    Bzoj 2064 分裂 题解
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10180332.html
Copyright © 2011-2022 走看看