N
Nick Coleman
I'm trying to write some code that will manipulate IP addresses,
particularly with a netmask, to generate valid network addresses and
valid host addresses.
For example, I would like to provide an IP address, apply a netmask, and
come up with valid network addresses, valid host addresses, broadcast
address, etc, in dotted decimal form.
I've tried things like:
// netmask = /8
long x = 0xff; // netmask in LSB
x = ~x // 'shift' netmask to MSB
netmask is not known, it is an input parameter to the method.
I know binary, and I've read Roedy's page on binary (thanks Roedy).
However, because I'm using maths to manipulate the mask and the
underlying IP address (to generate dotted decimal, for one thing), it
is failing as the most significant bit is '1', which means the long is
negative, therefore the maths is failing. This particularly applies to
converting dotted decimal and vice versa.
I've read java.net and InetAddress, but it seems oriented to real world
IP address space, whereas I am trying to manipulate IP addresses in a
theoretical space, suitable for doing things like setting up a network.
Is there an API class that knows about IP, netmask, dotted decimal, etc?
Many thanks,
particularly with a netmask, to generate valid network addresses and
valid host addresses.
For example, I would like to provide an IP address, apply a netmask, and
come up with valid network addresses, valid host addresses, broadcast
address, etc, in dotted decimal form.
I've tried things like:
// netmask = /8
long x = 0xff; // netmask in LSB
x = ~x // 'shift' netmask to MSB
netmask is not known, it is an input parameter to the method.
I know binary, and I've read Roedy's page on binary (thanks Roedy).
However, because I'm using maths to manipulate the mask and the
underlying IP address (to generate dotted decimal, for one thing), it
is failing as the most significant bit is '1', which means the long is
negative, therefore the maths is failing. This particularly applies to
converting dotted decimal and vice versa.
I've read java.net and InetAddress, but it seems oriented to real world
IP address space, whereas I am trying to manipulate IP addresses in a
theoretical space, suitable for doing things like setting up a network.
Is there an API class that knows about IP, netmask, dotted decimal, etc?
Many thanks,