- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 iPhone SDK,我让用户从图像选择器中选择图像。如果用户选择了他们之前选择过的图像,我想让用户意识到这一点。
我最初的计划(只是为了确保其他事情现在有效)是将图像保存到文件中(出于其他原因无论如何都需要这样做),使用 NSData 的校验和作为文件名。然后,当他们稍后选择相同的图像时,校验和将相同,因此我可以看到具有该名称的文件已经存在 - 欢呼!
但是,我已经在互联网和 Apple 文档中搜索了如何从 NSData 计算校验和。我知道我可以实现自己的实现,但如果可能的话,我宁愿避免这种情况。我也很高兴了解如何检查两个 UIImage 是否相同的其他想法。
编辑
两年前,我 promise 提供一个代码示例,现在就在这里。真的很抱歉耽误了! :)
+(NSString*)imageIdForData:(NSData*)data
{
char* result = (char*) [[data MD5Sum] bytes];
NSString* hash = [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1],
result[2], result[3],
result[4], result[5],
result[6], result[7],
result[8], result[9],
result[10], result[11],
result[12], result[13],
result[14], result[15]];
return hash;
}
最佳答案
因为有了类别一切都会变得更好......
标题:
@interface NSData (MD5)
- (NSString *)md5String;
@end
实现:
#import <CommonCrypto/CommonDigest.h>
- (NSString *)md5String
{
void *cData = malloc([self length]);
unsigned char resultCString[16];
[self getBytes:cData length:[self length]];
CC_MD5(cData, (unsigned int)[self length], resultCString);
free(cData);
NSString *result = [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
resultCString[0], resultCString[1], resultCString[2], resultCString[3],
resultCString[4], resultCString[5], resultCString[6], resultCString[7],
resultCString[8], resultCString[9], resultCString[10], resultCString[11],
resultCString[12], resultCString[13], resultCString[14], resultCString[15]
];
return result;
}
关于iphone - 从 NSData 在 iPhone 上计算校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1028742/
如何在 PHP 中生成 CRC-8 校验和? 最佳答案 function crcnifull ($dato, $byte) { static $PolyFull=0x8c; for ($i=0
我正在编写代码来使用 32 位无符号整数计算 CRC16。当尝试打印执行 CRC 操作的 XOR 函数的返回值时,它总是打印 0。我尝试了各种调试方法,例如打印语句,但是,我似乎无法弄清楚! 这是我的
ThinkPHP3.2.3验证码显示、刷新、校验 ,具体如下: 显示验证码 首先在Home/Controller下创建一个公共控制器PublicController
我想将自定义验证绑定(bind)到 TimePicker 自定义控件,但下面的代码显示“无法将内容添加到 TimePicker 的对象类型。”。
目录 Spring 校验(validator,JSR-303)实现 什么是JSR-303规范 与Spring MVC结合 实体类添加
导包和配置 导入 JSR 303 的包、hibernate valid 的包 ?
我是一名优秀的程序员,十分优秀!