Hi,
I need to compare 2 byte arrays and know which one is bigger or if they are equal (just equal or different is not enough). The byte arrays represent a String value of 15 characters or more. This comparison is repeated considerably in my code.
I would like to improve the bye array compare by using an equivalent of C++ memcmp method in Java (hopefully by JNI). I found an example to use DLLImport in C#, so I hope a JNI call can be applied as well.
Here is the C# code segment:
[DllImport("msvcrt.dll")]
unsafe static extern int memcmp(void* b1, void* b2, long count);
unsafe static int ByteArrayCompare1(byte[] b1, int b1Index, int b1Length, byte[] b2, int b2Index, int b2Length)
{
CompareCount++;
fixed (byte* p1 = b1)
fixed (byte* p2 = b2)
{
int cmp = memcmp(p1 + b1Index, p2 + b2Index, Math.Min(b1Length, b2Length));
if (cmp == 0)
{
cmp = b1Length.CompareTo(b2Length);
}
return cmp;
}
}
Does anyone know how to implement this in Java?
Thanks in advance,
Diana