-3

I have a Java Function which use Byte Array, C doesn't have this kind of data Type, How can i write that function in C.

I'm not good in C, Its a Little Confusing for me, I have byte hash1 and byte hash2.

It give me distance in int in java, Why do i need byte in Char in Java. I know the question title looks duplicate but, there is no clear explanation.

Java Code:

public static int distance(byte[] hash1, byte[] hash2) {
    int distance = 0;
    for (int i = 0; i < hash1.length && i < hash2.length; i++)
    {
        distance += ((hash2[i] & 0xFF) - (hash1[i] & 0xFF))*((hash2[i] & 0xFF) - (hash1[i] & 0xFF));
    }
    return distance;
}
Aqeel Haider
  • 603
  • 7
  • 24

1 Answers1

0

In C, you want unsigned char. You'll also need to pass the length of each array as a separate parameter.

public static int distance(unsigned char hash1[], unsigned char hash2[], int len1, len2) {
    int distance = 0;
    for (int i = 0; i < len1 && i < len2; i++)
    {
        distance += ((hash2[i] & 0xFF) - (hash1[i] & 0xFF))*
                    ((hash2[i] & 0xFF) - (hash1[i] & 0xFF));
    }
    return distance;
}
dbush
  • 205,898
  • 23
  • 218
  • 273