Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

IP address to long conversion

843829May 20 2005 — edited May 21 2005
Hi,

To reduce memory, I am trying to send IP address as long (conversion from doted to number) not as String (because string is a class and extra memory calls needed for cleaning in c) from my java to native JNI call. What I am doing is to convert using following algorithm in java then send to native JNI code:
// method converts an IP address (A.B.C.D) to an integer.
// The formula is: IP Number = A x (256*256*256) + B x (256*256) + C x 256 +  D
I print this long no. in java, and it displays correctly but in case of native JNI I am getting negative no (obvious over flow). I tried different ways to solve it but unsuccessful. How I can solve it?

Another important thing is inet_addr() function in win32 giving me different IP2Long conversion number instead what I am getting in java. Is there any difference of calculation algorithm or it’s a bit representation matter?
//native win32/BSD Socket 
//convert dotted IP/hostname into integer number format
unsigned long inet_addr(const char *cp)
Help Pls.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 18 2005
Added on May 20 2005
4 comments
925 views