Click here to Skip to main content
14,426,057 members
Rate this:
Please Sign up or sign in to vote.
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 6:40am
Comments
NotPolitcallyCorrect 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.
Tejas Sawant 13 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.
Tejas Sawant 13 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.
Tejas Sawant 13 19-Sep-16 4:07am
   
int SendText(int rowNo, const char *pText);

I have imported one dll in java project. I need to call above method how can I call that method.

In C# we can call above method like below

SendText(1, Marshal.StringToHGlobalAnsi("Hello"));

How can I do same in java ?

Richard MacCutchan 19-Sep-16 4:09am
   
You need to use the Java Native Interface, see Java SE 7 Java Native Interface-related APIs and Developer Guides[^].
Tejas Sawant 13 19-Sep-16 4:12am
   
Yes I have used JNI, I have imported dll successfully. But I am not able to call this method only.
Richard MacCutchan 19-Sep-16 4:19am
   
Sorry, but I have no idea what DLL you are trying to use or why you are unable to call some code. As I said above there is no simple equivalent Java to the C# code you have shown. You need to study the DLL and its documentation and write the necessary JNI code to enable you to access it from Java.
Tejas Sawant 13 19-Sep-16 4:38am
   
Hey Richard Thanks for comments.I appreciate your efforts.

DLL is developed in C . I am using that dll to communicate with hardware. So I need to call that method too.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100