NSData 与 NSData 的百分比比较 [英] NSData compare to NSData in percents

查看:41
本文介绍了NSData 与 NSData 的百分比比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有NSData *object1 和另一个 NSData *object2.如何比较这些对象的相似百分比?例如:Object1 与 Object2 的相似度为 - 99%.谢谢.

I Have NSData *object1 and another NSData *object2. How can I compare this objects by what percentage they are similar? For example: Object1 similar to Object2 in - 99%. Thanks.

推荐答案

获取两种情况下的字节,并通过检查它们中有多少相等来迭代.

Get the bytes in both cases and iterate through checking how many of them are equal.

uint8_t* bytes1 = (uint8_t*)[object1 bytes];
uint8_t* bytes2 = (uint8_t*)[object2 bytes];

NSUInteger sameCount = 0;
for (NSUInteger i = 0 ; i < MIN([object1 length], [object2 length]) ; ++i)
{
    if (bytes1[i] == bytes2[i])
    {
        sameCount++;
    }
}

double fractionSame = (double) sameCount / (double) MIN([object1 length], [object2 length]);

以上假设如果一个数据比另一个数据长,您不需要关心超出的部分.

The above assumes if one data is longer than the other, you don't care about the excess.

这篇关于NSData 与 NSData 的百分比比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆