How to get IP address from Hex String

M

M msgs

Hello

I have an IP Address returned to my application as the following
string 3264A8C0. Every 2 characters essentially form an octet in a
IPv4 address.

Using a calculator and knowing that every 2 characters form an IP
octet and are hex strings, the value computes to 50.100.168.192. I
would like to know if there are any C# .NET classes that can parse and
output this as a string in the form 192.168.100.50 (note the order is
reversed).

Thank you

M
 
P

Peter Duniho

M said:
Hello

I have an IP Address returned to my application as the following
string 3264A8C0. Every 2 characters essentially form an octet in a
IPv4 address.

Using a calculator and knowing that every 2 characters form an IP
octet and are hex strings, the value computes to 50.100.168.192. I
would like to know if there are any C# .NET classes that can parse and
output this as a string in the form 192.168.100.50 (note the order is
reversed).

You can use Int32.Parse() to parse the string as a hex-formatted integer.

If you need to reverse the bytes, the
IPAddress.NetworkToHostOrder(Int32) method can accomplish that for you.
Note, however, that the IPAddress constructor already assumes
big-endian ("network byte order"), such that the integer 0x3264A8C0
passed to the IPAddress(Int64) constructor will result in the IPAddress
of the form "192.168.100.50".

Pete
 
J

Jeff Johnson

Note, however, that the IPAddress constructor already assumes big-endian
("network byte order"), such that the integer 0x3264A8C0 passed to the
IPAddress(Int64) constructor will result in the IPAddress of the form
"192.168.100.50".

I'm confused by that statement. 0x3264A8C0 has 192 (C0) as the LSB, which
would suggest to me that the integer is in little-endian format.
 
P

Peter Duniho

Jeff said:
I'm confused by that statement. 0x3264A8C0 has 192 (C0) as the LSB, which
would suggest to me that the integer is in little-endian format.

Why would it do that? The IP address "192.168.100.50" isn't an integer
format to start with. You can't tell from that string whether the IP
address is represented as little- or big-endian.

As far as the statement I made goes, my point is that the
string-formatted integer "3264A8C0" parsed naturally by the
Int32.Parse() method winds up being stored as a 32-bit integer with the
value 0x3264A8C0. Again, no endian-ness can be inferred at this point.

But, we know that in .NET, integers are little-endian, while the
IPAddress(Int64) constructor expects big-endian where the LSB is the
last octet of the IP address. That is, the IPAddress(Int64) method
treats the passed in data as if it were big-endian, and uses the LSB of
the data as the least-significant octet of the IP address.

I admit that the statement I made is a bit confusing, because I am using
"endian" to describe data that doesn't really need to be interpreted as
having endian-ness. "Endian" really only applies to a sequence of
bytes; once that sequence of bytes is stored in a larger data type, such
as an Int32, it has no endian-ness until you want to write it back out
to some sequence of bytes.

In terms of .NET, this "larger data type" is an Int32 variable; at the
CPU level, it would be a register. Either way, there's no endian-ness
per se when the data is stored in that way. So technically my statement
isn't accurate.

But my point is that if one _does_ take a little-endian integer and
treat it as a sequence of bytes, the IPAddress(Int64) constructor then
turns around and interprets that sequence of bytes as if it were
big-endian (i.e. it reverses the bytes), _and_ it extracts the
least-significant octet of the IP address from the least-significant
byte of the integer.

Unfortunately, even that statement isn't quite precise, because the
IPAddress(Int64) constructor apparently also ignores leading zeroes in
the integer.

The bottom line here is that the OP need not reverse the bytes before
passing the parsed integer to the IPAddress(Int64) constructor.

Pete
 
B

Ben Voigt [C++ MVP]

The bottom line here is that the OP need not reverse the bytes before
passing the parsed integer to the IPAddress(Int64) constructor.

Why in the world is there an IPAddress(Int64) constructor?

IPv4 addresses fit in 32 bits, IPv6 need 128.
 
P

Peter Duniho

Ben said:
Why in the world is there an IPAddress(Int64) constructor?

I have no idea. But, it does make it easy for the constructor to ignore
the leading zeroes. :)
 
T

Tim Roberts

Jeff Johnson said:
I'm confused by that statement. 0x3264A8C0 has 192 (C0) as the LSB, which
would suggest to me that the integer is in little-endian format.

Ah, you have fallen cleanly into the endian trap by assuming what you are
trying to prove.

Endianness is only relevant when you look at the individual bytes as they
are stored in memory. 0x3264A8C0 as a dword is exactly the same in both
little-endian and big-endian forms. The KEY is that your IP address is
stored in memory like this:

C0 A8 64 32

If you fetched this into a 32-bit register on a big-endian machine, it
would be 0xC0A86432, which matches the order of the dotted notation.
 

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