zai zai ty bon
zai zai ty bon

Reputation: 145

how to compare two base64 string?

I have a base64 string(source string) which was converted from an image, I need a code to compare that string with another base64 string on web service and check which string have the most similarities to the source string,the language I use is C#, can anyone help me ???

Upvotes: 4

Views: 10316

Answers (2)

user1987824
user1987824

Reputation: 11

If you are looking for the total bits that are different, you can use something like this:

private long Base64BitsDifferent(string first64, string second64)
{
    long toReturn = 0;

    byte[] firstBytes = Convert.FromBase64String(first64);
    byte[] secondBytes = Convert.FromBase64String(second64);
    byte different = 0;

    for (int index = 0; index < firstBytes.Length; index++) {
        different = (firstBytes[index] ^ secondBytes[index]);

        while (different != 0) {
            toReturn++;
            different &= different - 1;
        }
    }

    return toReturn;
}

Assuming the number of bytes represented in both Base64 strings are equal.

Upvotes: -1

rick schott
rick schott

Reputation: 21127

You can compare the strings easily, you could also save some bandwidth by using a MD5 checksum on each end.

Finding "most similarities" is up your algorithm implementation. Only you know what "most similarities" means.

Upvotes: 3

Related Questions