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 4059148
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:10:15+00:00 2026-05-20T15:10:15+00:00

long ip2long( char *ip ) { long ip2long = 0; char B1[4], B2[4], B3[4],

  • 0
long ip2long( char *ip )
{
    long ip2long = 0;

    char B1[4], B2[4], B3[4], B4[4];
    int D1, D2, D3, D4 = 0;

    sscanf(ip, "%s.%s.%s.%s", B1, B2, B3, B4);

    D1 = atoi(B1);
    D2 = atoi(B2);
    D3 = atoi(B3);
    D4 = atoi(B4);

    if(D1 > 255 || D2  > 255 || D3 > 255 || D4 > 255)
        return 0;

    ip2long = D1*256*256*256+D2*256*256+D3*256+D4;

    return ip2long;
}

input data: 127.1.1.2

Why D1 == 127, but D2, D3 and D4 == 0?

— UPDATE —
Now code is

unsigned long ip2long( char *ip )
{
    unsigned long ip2long = 0;
    unsigned int D1, D2, D3, D4 = 0;

    sscanf(ip, "%u.%u.%u.%u", &D1, &D2, &D3, &D4);

    if(D1 > 255 || D2  > 255 || D3 > 255 || D4 > 255)
        return 0;

    ip2long = D1*256*256*256+D2*256*256+D3*256+D4;

    return ip2long;
}

Di is ok, but there are another troubles: result for 127.1.1.2 is 2130772226 instead of 2130772225 and result for 195.98.157.132 is -1016947324…
Why?

— Update 2 —
Thats ok, there was %d instread %u.

Kornel Kisielewicz thank you for telling about inet_addr function 🙂

PS sorry for my bad english =\

Question closed.

  • 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-20T15:10:15+00:00Added an answer on May 20, 2026 at 3:10 pm

    As fizzer notes, %s consumes the entire string – in this case dangerously copying it past the end of S1. There would have been a big clue if the sscanf() result had been compared to 4! Don’t assume things will work, especially when parsing input from outside the program. atoi ignores trailing garbage, so even with the %[^.] string format which will stop at a period it will process garbage values like 192,3.2x.3.1 by ignoring ,3 and x.

    fizzer’s suggestion of ("...%d...", &D1...) is a good start, but you must still compare the result to 4, otherwise invalid strings like “hello” would simply leave D1, D2, D3 and D4 unchanged. Negative values would be accepted. Note that only D4 is set to 0 – other values are not affected by the trailing = 0 on that line and may contain old garbage values form the stack which may or may not get through the later check against 255.

    Summarily:

    unsigned long ip2ulong(char *ip)
    {
        unsigned u1, u2, u3, u4;
        return sscanf(ip, "%u.%u.%u.%u", &u1, &u2, &u3, &u4) == 4 &&
               u1 <= 255 && u2 <= 255 && u3 <= 255 && u4 <= 255
               ? u1*256*256*256 + u2*256*256 + u3*256 + u4 : 0;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

long timeValue = timeInMillis(); int rand = timeValue%100 + 1; If we execute the
long a = (long)Math.pow(2, 32); // a = 4294967296 :) long a = (int)(long)Math.pow(2,
Long time lurker of stackoverflow and just trying something with iframes and jquery which
long time lurker, first question! I am struggling to optimize this query, which selects
Long story short, I would like to take my usercontrols that are loaded dynamically
long shot: is it possible to get the name of a calling function or
Long ago I have created a following template so that I get an assert
Long story short: I have 2 collections of objects. One contains good values (Let's
long-time listener, first-time caller! I’m trying to connect to MySQL using the ADO.NET Entity
Long story short : when mapping XML, is it better to have complex types

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.