# Convert network ip to ip2long

1. So if we have this: 2001:0000:85a3:0042:1000:0000:0000:7334
that would become: 2001:0000:85a3:0042:1000::7334
and the first 0000 will remain like that?

2. for example if we have this: 2c0f:ffd8:1::/48
this means 48 bits are for network and we should do 128 - 48 = 80 and it means the last 80 bits are for host?
so 80 times 1 binary would be: 1208925819614629174706175 and we need to convert this decimal into hex (ffffffffffffffffffff) to know the ending ip6v range? now what is ending range ip?
this
2c0f:ffd8:0001:ffff:ffff:ffff:ffff:ffff
is correct?

and this one:
2c0f:ffd8:8000::/33
128 - 33 = 95
2 ^ 95 - 1 = 39614081257132168796771975167
39614081257132168796771975167 = 7fff:ffff:ffff:ffff:ffff:ffff
7fff + 8000 = ffff
start = 2c0f:ffd8:8000::
end = 2c0f:ffd8:ffff:ffff:ffff:ffff:ffff:ffff

or how to calculate the ending range for cidr above?

1. It could be 2001:0000:85a3:0042:1000::7334. It could also be 2001:0:85a3:42:1000::7334.

2. Well it’d be simpler just to realize that every character is 4 bits (0000 through 1111, or 0 through F), and so 80 bits = 20 characters, and 48 would be 12 characters. But that’s helpful only when the network bits are a multiple of 4.

You’ll find it a lot easier to do the maths on smaller groups than the whole thing at once.

So if i were trying to do it programmatically? (off the top of my head)

Start: 2c0f:ffd8:1::/48
Slice off the / and find the value after it. (48). This is NetworkBits.
48/4 = 12, /4 (because there are 4 characters per ‘set’) = 3. I need 3 sets of values in the first half of my string to determine the range
My string is 2c0f:ffd8:1:: I dont see 39 (32 meaningful and 7 colons) characters here, so something’s been shortcutted.
Unshorthand function:
If :: exists in the string:
How many components are missing? Count the number of colons, and subtract 1. There are 3. That means the :: is representing a range of 5 sets of 0’s. Replace the :: with :0000:0000:0000:0000:0000:.
If the beginning or ending of my string are colons, remove them.
EndIf.
My string is now 2c0f:ffd8:1:0000:0000:0000:0000:0000 . I still dont see 39 characters, something’s still missing.
Explode the string on :. Foreach section, if the character count is less than 4, zero-pad the string.
My string is now 2c0f:ffd8:0001:0000:0000:0000:0000:0000. Hey, that looks like a properly formed IPv6 address I can work with.
End Function

(At this point i stopped doing pseudocode because i tried to code it myself and it took… exceedingly longer than it should have to wrap my head around the functions involved. The following is the result of my scratchings. There is PROBABLY a better way to do this.

``````//This Code assumes the existance of \$NetworkBits and \$string.
\$StartResult = array();
\$EndResult = array();
foreach(explode(':',\$string) AS \$char) {
\$NetworkBits = max(0,\$NetworkBits - 16);
}
echo implode(":",\$StartResult)."<br />".implode(":",\$EndResult);
``````

For \$NetworkBits = 48;
\$string = “2c0f:ffd8:0001:0000:0000:0000:0000:0000”;

``````2c0f:ffd8:0001:0000:0000:0000:0000:0000
2c0f:ffd8:0001:ffff:ffff:ffff:ffff:ffff
``````

For
\$NetworkBits = 33;
\$string = “2c0f:ffd8:8000:0000:0000:0000:0000:0000”;

``````2c0f:ffd8:8000:0000:0000:0000:0000:0000
2c0f:ffd8:ffff:ffff:ffff:ffff:ffff:ffff
``````

Of more interesting (maths wise, so you can see it’s not just ‘0 or f’) note would be
For
\$NetworkBits = 33;
\$string = “2c0f:ffd8:1000:0000:0000:0000:0000:0000”;

``````2c0f:ffd8:0000:0000:0000:0000:0000:0000
2c0f:ffd8:7fff:ffff:ffff:ffff:ffff:ffff
``````

(Though note should be made here that this string does not comply with CIDR, as the range would be represented as 2c0f:ffd8::\33, and not 2c04:ffd8:1::\33. The CIDR entry, if valid, should equal the Start of range.)

1. I did not try this yet, but seems interesting. https://github.com/lifo101/ip
I didn’t understand for composer download which version should I use in json file? https://packagist.org/packages/lifo/ip#dev-master
composer.json doesn’t accept blank for version, so is it ok I write “dev-master” as version?
“lifo/ip” : “dev-master” is fine? or “lifo/ip” : “~*”

2. gethostbyaddr works only with ipv4, I hope php7, adds necessary functions for gethostbyaddr_ipv6 and ipv6_2long etc.

The helper functions, are… pretty much what they say on the tin. Helper functions. They don’t add a new datatype to PHP, just do the math for you and return strings.