I'd like to conver long value to IP address, which fuction do I have to use?

S

somez72

hello.
I have a long value ,,, and
I'd like to conver it to IP address.
I can't find which function Should I use?

Could you someone help?


Have a day.
 
K

Karthik

somez72 said:
hello.
I have a long value ,,, and
I'd like to conver it to IP address.
I can't find which function Should I use?

Could you someone help?


Have a day.
Was wondering how would you represent an IP address as long ? Perhaps
String might do. Or better even a separate data structure for it as a
group of 'short's would do.
 
T

Thomas Schodt

somez72 said:
hello.
I have a long value ,,, and
I'd like to conver it to IP address.
I can't find which function Should I use?

Could you someone help?

An IPv4 address is 4 octets, or 32 bits.
In most C flavours that could be represented in a long.

A long in Java is 64 bits.

You are probably translating some legacy application from C to Java?

You can split your 32-bit scalar in 4 octets
with simple bit shifts and casts.
Store the 4 octets in a byte[4] array,
you can ignore the fact that byte is signed.

You can then use InetAddress.getByAddress(byte[])
to obtain an InetAddress (actually an Inet4Address).
 
S

somez72

Thanks.
what I mean was ,
Someone insert ip address to mysql DB with long format.
( for example , ip address field in mysql DB has values something
like 23455543222. )
so I need to convert it to IP address format ( xxx.yyy.zzz.ccc ) in my JAVA
application.

I'd like to know is there that kind of API, to convert to IP4 Adress in
JAVA?

Have a day.




Thomas Schodt said:
somez72 said:
hello.
I have a long value ,,, and
I'd like to conver it to IP address.
I can't find which function Should I use?

Could you someone help?

An IPv4 address is 4 octets, or 32 bits.
In most C flavours that could be represented in a long.

A long in Java is 64 bits.

You are probably translating some legacy application from C to Java?

You can split your 32-bit scalar in 4 octets
with simple bit shifts and casts.
Store the 4 octets in a byte[4] array,
you can ignore the fact that byte is signed.

You can then use InetAddress.getByAddress(byte[])
to obtain an InetAddress (actually an Inet4Address).
 

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
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top