Given two IPv6 addresses in string format ie "2001:0db8:0000:0000:0000:ff00:0042:8328" and "2001:0db8:0000:0000:0000:ff00:0042:8329"
how can I test if one is larger than the other? IPv4 was easy, convert into an integer value and compare, but IPv6 numbers are so large this becomes unfeasable.
Replace the address by ":" transform individually each term as hexadecimal value.
Compare the final result of each string.
function toHex(str) {
var hex = '';
for(var i=0;i<str.length;i++) {
hex += ''+str.charCodeAt(i).toString(16);
}
return hex;
}
Remove :
and do a hexa compare for the entire string.
var first = "2001:0db8:0000:0000:0000:ff00:0042:8328".replace(/:/g, "");
var second = "2001:0db8:0000:0000:0000:ff00:0042:8329".replace(/:/g, "");
// compare
如果你不介意使用第三方库,那就是ip-address 。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.