ip string to long and back again

C

coltrane

Does any class provide a method for converting a string ip address to a
long and long to ip address?

thanks


john
 
B

ByteCoder

coltrane said:
Does any class provide a method for converting a string ip address to a
long and long to ip address?

You mean the InetAddress class? Check the Java API. If it's possible,
the method is listed there.

PS. It isn't possible. You'd have to write it yourself to make it work
(sort of), but you'd better store it (the InetAddress object) in a byte
array using something like this:
byte[] myIPaddressBytes = myInetAddressObject.getAddress();
This way you can easily convert it back to an InetAddress using:
InetAddress myInetAddressObject =
InetAddress.getByAddress(myIPaddressBytes);
 
R

Ryan Stewart

coltrane said:
Does any class provide a method for converting a string ip address to a
long and long to ip address?
The other posts will help you along, but I'm curious how you intend to
represent an IP as a long. Does 127.0.0.1 become 127001? If so, then what
about 12.70.0.1?
 
B

ByteCoder

Ryan said:
The other posts will help you along, but I'm curious how you intend to
represent an IP as a long. Does 127.0.0.1 become 127001? If so, then what
about 12.70.0.1?

You could use leading zero's. So 127.0.0.1 would become: "127000000001"
 
V

Virgil Green

Ryan said:
The other posts will help you along, but I'm curious how you intend to
represent an IP as a long. Does 127.0.0.1 become 127001? If so, then
what about 12.70.0.1?

I have to assume that the OP meant something like this:

192.168.0.1 in Hex is
C0 A8 00 01, which in binary is
11000000 10101000 00000000 00000001,
which, when concatenated and treated as a long would be 3232235521 decimal.

- Virgil
 
S

Steve Horsley

Ryan said:
Duuuh. That didn't occur to me :)
Another thing that does not seem to have occured to anyone - a long is
not long enough to hold an IPv6 address. And any code that assumes that
IP addresses are only 4 bytes is broken.

Steve
 
B

ByteCoder

Steve said:
Another thing that does not seem to have occured to anyone - a long is
not long enough to hold an IPv6 address. And any code that assumes that
IP addresses are only 4 bytes is broken.

Thanks for the info. I thought a long had more memory available than an
int. Guess not.
 
M

Michiel Konstapel

Another thing that does not seem to have occured to anyone - a long is
Thanks for the info. I thought a long had more memory available than an
int. Guess not.

It does, 8 bytes vs 4 for an int. But an IPv6 address is 128 bits or 16
bytes.
Michiel
 
A

Ann

Michiel Konstapel said:
It does, 8 bytes vs 4 for an int. But an IPv6 address is 128 bits or 16
bytes.
Michiel

Does IPv6 use dot notation? If so, are the number ranges larger or
are there more numbers, (or both?)
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top