IP address to binary conversion

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 16065 Views - Last Post: 11 March 2014 - 08:39 AM Rate Topic: -----

#16 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7199
  • View blog
  • Posts: 15,008
  • Joined: 16-October 07

Re: IP address to binary conversion

Posted 11 March 2014 - 08:39 AM

Thought I'd add that infernorthor was spot on with this. You're parsing numbers, they are numbers, and then you want to validate them.

I'd add an extra constraint and a check:
int parseIp(const char *s, unsigned *n) {
    unsigned a[4];
    if (sscanf(s, "%u.%u.%u.%u", a, a+1, a+2, a+3)!=4) {
        return 0;
    } else {
        /*
        loop through each value
        if it's > 255, it's wrong, return 0
        otherwise, sum it up to your result
        */


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2