# IP address to binary conversion

• (2 Pages)
• 1
• 2

## 15 Replies - 14412 Views - Last Post: 11 March 2014 - 08:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=342029&amp;s=0ec7ac31f85d96b38dd0506cb2100928&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 osu_16

Reputation: 0
• 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

• Engineer

Reputation: 1270
• Posts: 4,998
• 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?

### #3 osu_16

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

## Re: IP address to binary conversion

Posted 10 March 2014 - 08:28 PM

Yes

### #4 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• 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.

### #5 osu_16

Reputation: 0
• 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;
}

```

### #6 infernorthor

• D.I.C Lover

Reputation: 362
• 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);

```

### #7 osu_16

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

## Re: IP address to binary conversion

Posted 10 March 2014 - 11:10 PM

infernorthor, 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?

### #8 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• 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

### #9 infernorthor

• D.I.C Lover

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

## Re: IP address to binary conversion

Posted 10 March 2014 - 11:22 PM

osu_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

### #10 osu_16

Reputation: 0
• 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)

```

### #11 AKMafia001

• </code.in.dream>

Reputation: 238
• 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

### #12 Skydiver

• Code herder

Reputation: 6011
• Posts: 20,672
• Joined: 05-May 12

## Re: IP address to binary conversion

Posted 11 March 2014 - 06:15 AM

osu_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.

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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.

### #14 Skydiver

• Code herder

Reputation: 6011
• Posts: 20,672
• 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.

### #15 osu_16

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

## Re: IP address to binary conversion

Posted 11 March 2014 - 06:41 AM

Skydiver, 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.