返回信息流本人纯iOS开发新手,最近遇到一个问题:
我想法很简单——一个TextField做输入字符数字用
一个Label显示MD5 Hash之后的值
一个按钮用于触发MD5计算操作。
目前已经实现的,是我可以在代码中指定一串字符,用按钮可以在Label中正确显示MD5值。
但是等我试图实现计算TextField中的值的时候,问题来了,计算出来的MD5值明显不正确。
我想知道是因为TextField.text这个得到的值不是String类型么?可是我做了类型转换之后再MD5,值还是那个错误的值。
也在CocoaChina上发过帖子问。。。不过就是迄今为止问题还没解决
CocoaChina的帖子: http://www.cocoachina.com/bbs/read.php?tid=59754&page=e&#a
帖子回复中的方法我试过,结果仍然是一样的……
恳请各位大牛帮忙!
我的.m文件代码如下,请指教
//  Hello_MD5ViewController.m
#import "Hello_MD5ViewController.h"
#import <CommonCrypto/CommonDigest.h> //Import for CC_MD5 access
NSString* md5(NSString *str)
{
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5(cStr, strlen(cStr), result); //This is the MD5 calculate
return [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]
];
}
@implementation Hello_MD5ViewController
@synthesize md5Text;
- (IBAction)buttonPressed:(id)sender {
NSString *input = plainText.text;
NSString *digest = md5(input);
NSString *md5Result = [[NSString alloc] initWithFormat:@"MD5 RESULT \n%@", digest];
md5Text.text = md5Result;
}
- (void)dealloc
{
[plainText release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.md5Text = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #3322同步于 2011/5/19
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖
[iPhone]如何从TextField导入数据做MD5计算
ss1271
2011/5/19镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
【 在 kmplayer 的大作中提到: 】
: 是不是需要UITextField 的delegate连接到File owner么?
: --
于是我发现我的问题所在是我把plaintext和plainText搞混了,我做了IB链接的是plainText,前者是自己笔误写成了小写于是就悲剧了……