This project is read-only.

ReadOnlyCollection<byte> to IpV4Address

Dec 29, 2011 at 7:37 PM

Hello,

This is what I have done.

IpV4Address tempTargetIP = new IpV4Address("192.168.0.1");

byte[] targettIP = new byte[4];
targettIP.Write(0, tempTargetIP, Endianity.Big);

arpLayer.TargetProtocolAddress = new ReadOnlyCollection<byte>(targettIP)


Now, my problem is, I do not know how to convert from "TargetProtocolAddress" back to "IpV4Address" class. Then, I would like to convert it to string.

I tried using "arpLayer.TargetProtocolAddress.ToString()" but got this:


"System.Collections.ObjectModel.ReadOnlyCollection1[System.Byte]"

I have to do this in one part of my program. I hope somebody here could help. Your help is much appreciated. Thank you.

Dec 30, 2011 at 10:42 AM

Hi valthyx,

 

I think a simple way to do it would be

 

new IpV4Address(arp.TargetProtocolAddress.ToArray().ReadUInt(0, Endianity.Big));

 

I hope this helps,

 

Boaz.

Dec 30, 2011 at 12:21 PM

Thank you very much Boaz. It works well with "ToString()".

Dec 31, 2011 at 5:04 PM

It works well with IpV4Addres. However, when I tried to use it with MacAddress, it doesn't work. I tried

"new MacAddress(arp.TargetHardwareAddress.ToArray().ReadUInt(0, Endianity.Big)).ToString()"

It gives me a different MAC address than the expected MAC Address. Thank you.

Dec 31, 2011 at 7:23 PM

Try

 

new MacAddress(arp.TargetHardwareAddress.ToArray().ReadUInt48(0, Endianity.Big)).ToString()

 

I hope this helps,

 

Boaz.

Jan 1, 2012 at 9:43 PM

Thank you again. It works again. :)