Django js code realizes the mutual conversion between dotted IP and integer value

Problems found: by django js code to pass route when the URL parameters, discover routes could not be resolved correctly points acquired point value IP
 solution: to be js dotted IP conversion, the integer value to the parameter passing to djangoURL following is The conversion function between dotted IP and integer value: 
// Convert dotted ip to integer value 
function ipToInt(ip){ 
    let num = 0; 
    ip = ip.split("."); 
    num = Number( ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]); 
    num = num >>> 0; 
    return num; 
} 
// Convert integer values ​​to dots ip 
function intToIP(num){ 
    let str; 
    let tt = new Array(); 
    tt[0] = (num >>> 24) >>> 0; 
    tt [1] = ((num << 8) >>> 24) >>> 0; 
    tt[2] = (num << 16) >>> 24; 
    tt[3] = (num << 24)>>> 24;




    str = String(tt[0]) + "." + String(tt[1]) + "." + String(tt[2]) + "." + String(tt[3]);
    return str;
}

When using, you can directly create a variable to receive

 

Guess you like

Origin blog.csdn.net/lyw5200/article/details/114654809