byte[] to IPv4 IPAddress

N

Noël Danjou

Hello,

I need to mask some bits of an IP address that I initially get as a string
so I do something like that:

IPAddress addrLo = IPAddress.Parse("172.16.0.0");
byte[] bytes = addrLo.GetAddressBytes();

Then I do some processing (mask some bits) on the "bytes" byte array and
finally I need to convert that byte array back to an IPAddress (that must
work for both IPv4 and IPv6 addresses).

For IPv6 I can use the following syntax:

addrHi = new IPAddress(bytes);

Unfortunately that does not work with IPv4 addresses (ArgumentException), so
I wrote something like that:

if (addrLo.AddressFamily == AddressFamily.InterNetworkV6)
{
// Conversion: Text(addrLo) -> Binary (addrHi)
// Only works with IPv6 addresses
addrHi = new IPAddress(bytes);
}
else
{
// Not quite efficient
// Conversion: Text(addrLo) -> Binary -> Text (addr) -> Binary (addrHi)
string addr = String.Format("{0}.{1}.{2}.{3}", bytes[0], bytes[1],
bytes[2], bytes[3]);
addrHi = IPAddress.Parse(addr);
}

Is there a better way to convert byte arrays to an IPv6/IPv4 IPAddress?
Thank you.
 
A

Andreas Håkansson

Noël,

You could use the following code to convert your byte array
into a string and then create an IPAddress.

ipString = ASCIIEncoding.ASCII.GetString(bytes)
IPAddress myAddress = IPAddress.Parse(ipString);

Hope this helps,

//Andreas
 
M

mikeb

Noël Danjou said:
Hello,

I need to mask some bits of an IP address that I initially get as a string
so I do something like that:

IPAddress addrLo = IPAddress.Parse("172.16.0.0");
byte[] bytes = addrLo.GetAddressBytes();

Then I do some processing (mask some bits) on the "bytes" byte array and
finally I need to convert that byte array back to an IPAddress (that must
work for both IPv4 and IPv6 addresses).

For IPv6 I can use the following syntax:

addrHi = new IPAddress(bytes);

Unfortunately that does not work with IPv4 addresses (ArgumentException), so

Boy, MS should fix that!

I wrote something like that:

if (addrLo.AddressFamily == AddressFamily.InterNetworkV6)
{
// Conversion: Text(addrLo) -> Binary (addrHi)
// Only works with IPv6 addresses
addrHi = new IPAddress(bytes);
}
else
{
// Not quite efficient
// Conversion: Text(addrLo) -> Binary -> Text (addr) -> Binary (addrHi)
string addr = String.Format("{0}.{1}.{2}.{3}", bytes[0], bytes[1],
bytes[2], bytes[3]);
addrHi = IPAddress.Parse(addr);
}

Is there a better way to convert byte arrays to an IPv6/IPv4 IPAddress?
Thank you.

This will work with IPv4 addresses, but it won't work with IPv6
addresses, so you'll still have to do it 2 different ways:

IPAddress addrHi = new IPAddress(
(long) BitConverter.ToUInt32( bytes, 0));


I haven't had to deal with IPv6 addresses, yet. It looks like MS should
take another look at the IPAddress class to make it work better for
both address families.
 
J

Jeffrey Tan[MSFT]

Hi noel,

Thanks for your posting!!

Yes, I have found this is a bug in IPAddress(byte[]) constructor, it may be
fixed by Whidbey Beta 1.

Also, as I see that you have found a workaround for this issue. If you need
further help, please feel free to post, I will help you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
N

Noël Danjou

Thank you all for your replies. I'm glad to read that the IPAddress(byte[])
constructor will be fixed. For now I changed my code like this:

if (lo.AddressFamily == AddressFamily.InterNetworkV6)
{
hi = new IPAddress(hibytes);
}
else
{
hi = new IPAddress((long)BitConverter.ToUInt32(hibytes, 0));
}

Best regards,
 
J

Joris Dobbelsteen

IPAddress addrLo = IPAddress.Parse("172.16.0.0");

Thank you, this is just what I needed. I wasn't even able to perform a
simple task as sending a UDP packet over the network using the sockets.

Surely Microsoft's MSDN Library really degraded since .NET. Hopefully it
will become better soon, including some more samples of even simple tasks.
Next references to the methods you actually needed (instead of guessing you
probably needed this one).

- Joris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top