Jump to content

convert Ipv4 and IpV6 to netmask


edotom

Recommended Posts

Hi

I want to convert ipv4 and Ipv6 addresses to netmask lenght like:

255.255.0.0  

result 

16

 

I was thinking in splitting into arrays and find the byte and then just count the number of bits

It this possible?

 

thanks in advance

Link to comment
Share on other sites

Then you'll probably have to make your own function to deal with it. Since the problem is simply finding the number of leading 1 bits I'd just go with a function to do both IPv4 and IPv6 that gives you exactly the result you need. Means you don't have to parse the address for real, simply count how many 1 bits you find until the first 0.

"Simply". Thing is that there are a few valid formats you'd have to account for: pieces not having all four hex digits, omitted zeroes with a ::, a trailing IPv4 address... Unless you can restrict the input.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.