如何比较两个base64字符串?

时间:2013-01-09 17:19:08

标签: c# asp.net

我有一个从图像转换的base64字符串(源字符串),我需要一个代码来将该字符串与web服务上的另一个base64字符串进行比较,并检查哪个字符串与源字符串最相似,语言为使用是C#,任何人都可以帮助我???

2 个答案:

答案 0 :(得分:3)

您可以轻松比较字符串,也可以通过在每端使用MD5校验和来节省一些带宽。

查找“最相似”是你的算法实现。只有你知道“最相似”的含义。

答案 1 :(得分:0)

如果您要查找不同的总位数,可以使用以下内容:

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;
}

假设两个Base64字符串中表示的字节数相等。

相关问题