Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:21:05+00:00 2026-05-27T07:21:05+00:00

In CoffeeScript how would I go about converting an IP (standard IPv4 127.0.0.1) into

  • 0

In CoffeeScript how would I go about converting an IP (standard IPv4 127.0.0.1) into an integer?

EDIT: Lots of great answers here, thanks everyone!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:21:05+00:00Added an answer on May 27, 2026 at 7:21 am

    EDIT: Coffeescript

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

    which compiles 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 pure 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 pure Javascript implementation 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;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a question about what I would think would be a simple pattern
I'm using CoffeeScript in a Rails application, and I would like to unit test
I am quite new to vim and I am trying to edit coffeescript. Whenever
I am a newbie in Coffeescript and would like to use the delegate() method
I would like to ask how other more experienced Coffeescript developers are using their
Personally Coffeescript is ok, but I would rather it be a supporting player in
I have a .coffeescript.erb file in which I would like to get CSRF meta-tag
I've been reading up a bit about coffeescript's inheritance model and I have the
I would like to make a Cakefile task to watch some CoffeeScript files just
I would like to create visualizations using Protovis, but writing in CoffeeScript rather than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.