I’m deserializing data from a web page generated by php that is using ip2long(). However, when I try to make a new ip address by using the integer value in the constructor of IPAddress the dotted version of the ip address is in reverse order?
ex:
4.3.2.1 should really be 1.2.3.4
Any ideas of how to fix this?
It sounds like someone is using little-endian and someone is using network byte order (big-endian) for the packed value. For instance the octect sequence compromising an integer,
AA,BB,CC,DDin LE isDD,CC,BB,AAin BE/NBO — a nice symmetrical reverse!Since the IPAddress(Int64) constructor documentations says:
I would imagine that
ip2longin PHP is generating a value in little-endian. Good thingIPAddressalso takesbyte[]for the constructor, now get those elbows greasy… just pass the bytes in the “correct” order.Happy coding.
The code at How to convert an int to a little endian byte array? should give some ideas.
Or, as Josh points out, there is a HostToNetworkOrder method to do this.