IP address to binary conversion

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 osu_16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-March 14

IP address to binary conversion

Posted 10 March 2014 - 08:17 PM

Does anybody have any linkable to resources to provide on converting an IP address to a binary number? As in the entire thing 123.45.555.49 to it's binary equivalent.
Is This A Good Question/Topic? 0
  • +

Replies To: IP address to binary conversion

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1269
  • View blog
  • Posts: 4,997
  • Joined: 09-June 09

Re: IP address to binary conversion

Posted 10 March 2014 - 08:26 PM

Do you know how to convert decimal to binary?
Was This Post Helpful? 1
  • +
  • -

#3 osu_16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-March 14

Re: IP address to binary conversion

Posted 10 March 2014 - 08:28 PM

Yes
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1269
  • View blog
  • Posts: 4,997
  • Joined: 09-June 09

Re: IP address to binary conversion

Posted 10 March 2014 - 08:43 PM

An IPv4 Address is just a 4 byte integer A.B.C.D. To convert to binary, simply convert A, B, C, D from decimal to binary.
Was This Post Helpful? 1
  • +
  • -

#5 osu_16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-March 14

Re: IP address to binary conversion

Posted 10 March 2014 - 10:47 PM

I want to search an IP address up to the "/" and take everything before the "/" so I can convert it from a char to an int. The length up to the "/" is not constant. Then take that int and convert it to binary. How would I read everything before the "/", store it in a variable, and then continue to convert it to binary. I'm aware of `strlen` but don't think that will work with my situation?

My code:
int main(int argc, char** argv)
{
     char *s;
     char buf[] = "10.29.246.49/32";
     s = strchr(buf, '/');
     if (s != NULL)
          printf("found '/' at %s\n", s);
     return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: IP address to binary conversion

Posted 10 March 2014 - 11:00 PM

How about you try using formats

     char s[10];
     int IP[4];
     char buf[] = "10.29.246.49/32";
    sscanf(buf,"%d.%d.%d.%d/%s",&IP[0],&IP[1],&IP[2],&IP[3],&s);


Was This Post Helpful? 3
  • +
  • -

#7 osu_16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-March 14

Re: IP address to binary conversion

Posted 10 March 2014 - 11:10 PM

View Postinfernorthor, on 10 March 2014 - 11:00 PM, said:

How about you try using formats

     char s[10];
     int IP[4];
     char buf[] = "10.29.246.49/32";
    sscanf(buf,"%d.%d.%d.%d/%s",&IP[0],&IP[1],&IP[2],&IP[3],&s);



So an ip address is no longer than 10 digits right?
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1269
  • View blog
  • Posts: 4,997
  • Joined: 09-June 09

Re: IP address to binary conversion

Posted 10 March 2014 - 11:18 PM

That is CIDR notation. The last two digits represent the length of the routing prefix and subnet. The max is 32 for ipv4.

This post has been edited by jjl: 10 March 2014 - 11:18 PM

Was This Post Helpful? 1
  • +
  • -

#9 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: IP address to binary conversion

Posted 10 March 2014 - 11:22 PM

View Postosu_16, on 10 March 2014 - 11:10 PM, said:

So an ip address is no longer than 10 digits right?


No, I just put a random number.

This post has been edited by infernorthor: 10 March 2014 - 11:23 PM

Was This Post Helpful? 0
  • +
  • -

#10 osu_16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-March 14

Re: IP address to binary conversion

Posted 11 March 2014 - 01:09 AM

I used 'strlen' to find the length of a string, call it string a. I then did some other things to create a binary string. The binary strings value is longer than string a. I want to return the binary string as long as string a. How would I do that?
Let me try to code it out to maybe help clarify:
     
     int j;
     char *s, *binary_string;
     prefix_length = strlen(s);
     for(i = 4; i > 0; i--){
          dot = strtok(s, ".");
          if(dot != NULL){
               j = atoi(dot);
               binary_string += dec2bin(j);
          }
          else
               ....
     return_val = (all data of binary string, the length of prefix_length)


Was This Post Helpful? 0
  • +
  • -

#11 AKMafia001  Icon User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: IP address to binary conversion

Posted 11 March 2014 - 03:41 AM

You can use strncpy() to copy up to n bytes... But remember, you said that source (i.e. binary_string) is large than destination... So, you will have to confirm that the destination string is properly null terminated '\0'...

For example:
char dst[SIZE+1];
strncpy(dst, src, n);
dst[n] = '\0';

// OR
char dst[SIZE];
strncpy(dst, src, SIZE - 1);
dst[SIZE - 1] = '\0';



Hope it Helps!

This post has been edited by AKMafia001: 11 March 2014 - 03:44 AM

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: IP address to binary conversion

Posted 11 March 2014 - 06:15 AM

View Postosu_16, on 11 March 2014 - 04:09 AM, said:

I then did some other things to create a binary string. The binary strings value is longer than string a. I want to return the binary string as long as string a. How would I do that?

You simply can't do that.

Consider the IP address in the string "192.168.1.1". That is 11 characters. Each of the 4 octets will take up 8 characters of a binary string. 8 times 4 is 32 characters:
     192      168        1        1    decimal
   C   0    A   8    0   1    0   1    hexadecimal
11000000 10101000 00000001 00000001    binary



Now, on the other hand, if you converted the string digits of IP address to a binary value then you'll have more luck. The same string above has 11 characters (not counting the null terminator), but 32 bits is only 4 bytes.
Was This Post Helpful? 1
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: IP address to binary conversion

Posted 11 March 2014 - 06:20 AM

I merged all your related topics into a single one. Please keep your questions on this project within this topic while you're dealing with this specific issue.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: IP address to binary conversion

Posted 11 March 2014 - 06:24 AM

You are using strtok() in your other post. You can use that in combination with strcpy(). Or you could use strchr() as in your other post in combination with strncpy() and some pointer math as suggested.

Please don't take this the wrong way, but it feels like you are grabbing random bits of code of off the Internet and then throwing them together in hopes that something will work. That or you are in a class right now about network programming, but missed a prerequisite class about C programming.
Was This Post Helpful? 0
  • +
  • -

#15 osu_16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-March 14

Re: IP address to binary conversion

Posted 11 March 2014 - 06:41 AM

View PostSkydiver, on 11 March 2014 - 06:24 AM, said:

Please don't take this the wrong way, but it feels like you are grabbing random bits of code of off the Internet and then throwing them together in hopes that something will work. That or you are in a class right now about network programming, but missed a prerequisite class about C programming.


I'm not taking it the wrong way because I could see where your are coming from. But just so you know, you have it wrong. Yes, I am grabbing bits of code. But they are not random. If I were doing a puzzle, I'm not taking an obvious edge and trying to fit it to an obvious non-edge piece. Since the answer isn't there, I look at different bits of code in relation to my problem, do the research I can to find out if they work, and if I think they do work I attempt to try to combine them as well as hop on these type of sites in hopes somebody can help me out. This is for a class, an operating systems class. The two classes I've taken prior to this do not cover C to this extent. At the same time, I'm not a super fast learner of C and it's been maybe 6 months or longer since I've taken a class involving C.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2