Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have put lot of efforts, googled and tried all possible ways. But I did not got satisfaction so I come here finally.

I want to convert below line of code [C#] into java.

1] Method used to convert IP into uint
private uint GetIP(string strIp)
       {
           System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse(strIp);
           uint lIp = (uint)ipaddress.Address;
           
           lIp = ((lIp & 0xFF000000) >> 24) + ((lIp & 0x00FF0000) >> 8) + ((lIp & 0x0000FF00) << 8) + ((lIp & 0x000000FF) << 24);
           MessageBox.Show(strIp + " = " + lIp);
           return (lIp);
       }



2] Method used to convert String into IntPtr

IntPtr iPtr = Marshal.StringToHGlobalAnsi("Hello");


Please help me as soon as possible. Thanks in advance.

I really appreciate help.

What I have tried:

I have tried following code for converting IP to Long, not sure is it right or wrong .

public static long ipToLong(String ipAddress) {

String[] ipAddressInArray = ipAddress.split("\\.");

long result = 0;
for (int i = 0; i < ipAddressInArray.length; i++) {

int power = 3 - i;
int ip = Integer.parseInt(ipAddressInArray[i]);
result += ip * Math.pow(256, power);

}

return result;
}

For String to IntPtr, tried to convert String to Ascii and then tried to use Intger by reference.
Posted
Updated 18-Sep-16 5:40am
Comments
[no name] 18-Sep-16 8:13am    
If you not sure if it's right or wrong, why don't you go off and try it first.
[no name] 18-Sep-16 12:37pm    
I want suggestions and opinions so I write here.
Richard MacCutchan 19-Sep-16 3:14am    
I gave you a suggestion yesterday; go and try it.
[no name] 19-Sep-16 3:36am    
Thanks for great suggestion. Code is working fine I was asking does it right approach or something else need to do.

Please suggest some java code for below C# line of code

IntPtr iPtr = Marshal.StringToHGlobalAnsi("Hello");
Richard MacCutchan 19-Sep-16 3:39am    
There is not equivalent in Java. I suggest you read the sections on internet message handling in the Java documentation.

1 solution

 
Share this answer
 
Comments
Maciej Los 19-Sep-16 2:06am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900