How to convert IPv4 to Integer using CoffeScript?

In CoffeeScript, how do I go about converting IP (standard IPv4 127.0.0.1) to an integer?

EDIT: There are a lot of great answers here, thanks everyone!

+6
source share
7 answers

UPDATE: Coffeescript

ipStringToInteger = (x) -> res = 0 (res = res * 256 + Number(y) for y in x.split(".")) res 

which boils down to

 var ipStringToInteger; ipStringToInteger = function(x) { var res, y, _i, _len, _ref; res = 0; _ref = x.split("."); for (_i = 0, _len = _ref.length; _i < _len; _i++) { y = _ref[_i]; res = res * 256 + Number(y); } return res; }; 

A short clean javascript implementation is

 var ipV4StringToInteger = function(string) { var parts = string.split("."); var sum = 0; for(var i = 0; i < 4; i++) { var partVal = Number(parts[i]); sum = (sum << 8) + partVal; } return sum; }; 

A good implementation of pure Javascript is

 var ipV4StringToInteger = function(string) { var parts = string.split("."); if(parts.length != 4) throw new Error("IPv4 string does not have 4 parts."); var sum = 0; for(var i = 0; i < 4; i++) { var part = parts[i]; if(!part.match("^\\d+$")) throw new Error("Non-digit, non-period character in IPv4 string."); var partVal = Number(part); if(partVal > 255) throw new Error("IPv4 string contains invalid value."); sum = ((sum << 8) + partVal) >>> 0; } return sum; }; 
+3
source

I will take the bitrate approach:

 ip_to_int = (value) -> result = 0 for part, i in value.split "." result |= part << (3-i) * 8 result 

Using it is simple:

 alert ip_to_int "127.0.0.1" 
+2
source

To convert ip to integer you need a formula

 (first octet * 256³) + (second octet * 256²) + (third octet * 256) + (fourth octet) 

Let ip = '127.0.0.1' , which can be written as:

 integer = 0 for octet, i in ip.split('.') integer += octet * Math.pow 256, 3-i 

And it can be simplified using the reduce method:

 integer = ip.split('.').reduce ((t, n) -> t*256 + parseInt n), 0 
+2
source

I believe that @ellisbben hit it, but thought I would switch to another version of JS with even more error checking.

 function ip2int(ip){ // split them in to their own numbers var octets = ip.split('.'); // make sure we have a valid IP. (length-wise) if (octets.length!=4) return false; // begin parsing var result = 0; for (var v = 1; v <= 4; v++){ var i = parseInt(octets[v-1],10); // valid number? if (isNaN(i) || i < 0 || i > 255) return false; result += (i * Math.pow(256,(4-v))); } return result; } alert(ip2int('127.0.0.1')); 
+1
source

A different bit shifting method is used here.

 addr='192.168.5.253'.split('.'); ipInt = (+addr[0] << 24) + (+addr[1] << 16) + (+addr[2] << 8) + (+addr[3]); 

And cancel it ...

 [ipInt >> 24 & 0xff,ipInt >> 16 & 0xff,ipInt >> 8 & 0xff,ipInt & 0xff].join('.'); 
0
source

Sorry, this is the JS version, but it is very easy to convert it to CoffeeScript:

 // 'xxxx' -> number function Ip4ToInt(ip4str) { var addr = ip4str.split('.'); if ( addr.length !== 4 || addr.some( (elm) => (parseInt(elm) > 255 || parseInt(elm) < 0 ) ) ) { throw new Error('Invalid ip4 string: ' + ip4str); } return ((addr[0] << 24) >>> 0) + ((addr[1] << 16) >>> 0) + ((addr[2] << 8) >>> 0) + (addr[3]); } // number -> 'xxxx' function IntToIp4(ip4int){ return [ip4int >> 24 & 0xff,ip4int >> 16 & 0xff,ip4int >> 8 & 0xff,ip4int & 0xff].join('.'); } 
0
source

Smallest solution for node.js:

 function ip2int(ip) { var ipint = 0; ip.split('.').map(function (e, i) { ipint += Math.pow(256, 3-i) * e; }); return ipint; } console.log(ip2int('10.1.1.20')); 
-1
source

Source: https://habr.com/ru/post/903152/


All Articles