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

查看:116
本文介绍了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天全站免登陆