Iphone 使用Textfield的输入值计算MD5
我在尝试传递Textfield的输入字符串以计算MD5时遇到了一个问题 当我在代码中只给出一个指定的字符串(如abc)并进行MD5计算时,它将返回正确的结果 当我尝试使用textfield让用户输入相同的字符串abc,然后我将textfield.text传递给md5函数以执行md5哈希时,出现了问题。这一次,结果不同了 我完全被这个问题弄糊涂了,已经在那里呆了将近一个星期,但就是不知道为什么以及如何解决它 你能帮我一下吗 这是我的密码: 您好\u MD5ViewController.hIphone 使用Textfield的输入值计算MD5,iphone,md5,textfield,Iphone,Md5,Textfield,我在尝试传递Textfield的输入字符串以计算MD5时遇到了一个问题 当我在代码中只给出一个指定的字符串(如abc)并进行MD5计算时,它将返回正确的结果 当我尝试使用textfield让用户输入相同的字符串abc,然后我将textfield.text传递给md5函数以执行md5哈希时,出现了问题。这一次,结果不同了 我完全被这个问题弄糊涂了,已经在那里呆了将近一个星期,但就是不知道为什么以及如何解决它 你能帮我一下吗 这是我的密码: 您好\u MD5ViewController.h // /
//
// Hello_MD5ViewController.h
// Hello-MD5
//
//
#import <UIKit/UIKit.h>
@interface Hello_MD5ViewController : UIViewController {
UILabel *md5Text;
UITextField *plainText;
}
@property (nonatomic, retain) IBOutlet UILabel *md5Text;
- (IBAction)buttonPressed: (id)sender;
@end
// Hello-MD5
//
//
#import <UIKit/UIKit.h>
/*@interface NSString (md5Extension)
- (NSString *) md5;
@end
@interface NSData (md5Extension)
- (NSString *) md5;
@end
*/
@interface Hello_MD5ViewController : UIViewController {
UILabel *md5Text;
UITextField *plainText;
}
//@property (nonatomic, retain) NSString *input;
@property (nonatomic, retain) IBOutlet UILabel *md5Text;
- (IBAction)buttonPressed: (id)sender;
@property (nonatomic, retain) IBOutlet UITextField *plaintext;
@end
//
//您好\u MD5ViewController.h
//你好,MD5
//
//
#进口
@接口Hello_MD5ViewController:UIViewController{
UILabel*md5Text;
UITextField*纯文本;
}
@属性(非原子,保留)IBUILabel*md5Text;
-(iAction)按钮按下:(id)发送者;
@结束
您好\u MD5ViewController.m
//
// Hello_MD5ViewController.m
// Hello-MD5
//
#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 call
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 = [[NSString alloc] initWithString: plainText.text];
NSString *digest = md5(input); //if I use textfield.text to passing the string, the result will be wrong
//NSString *digest = md5(@"123"); //if I give a string within code like this, it'll return correct result
NSString *md5Result = [[NSString alloc] initWithFormat:
@"MD5 RESULT \n%@", digest];
md5Text.text = md5Result;
//Calculate MD5 value
}
- (void)dealloc
{
[plainText release];
[md5Text 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
// Hello-MD5
#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 call
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;
@synthesize plaintext;
- (IBAction)buttonPressed:(id)sender {
//NSString *input = [[NSString alloc] initWithString: plainText.text];
if(plainText.text == nil)
{
NSLog(@"Disconnected.");
}
//NSLog(@"Output %@",plainText.text);
//NSString *digest = md5(input);
//NSString *digest = md5(@"123");
//NSString *md5Result = [[NSString alloc] initWithFormat:
// @"MD5 RESULT \n%@", digest];
//md5Text.text = md5Result;
//Calculate MD5 value
}
- (void)dealloc
{
[plainText release];
[md5Text release];
//[input 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
//
//您好\u MD5ViewController.m
//你好,MD5
//
#导入“Hello_MD5ViewController.h”
#导入//导入用于CC_MD5访问
NSString*md5(NSString*str)
{
常量字符*cStr=[str UTF8String];
无符号字符结果[16];
CC_MD5(cStr,strlen(cStr),result);//这是MD5调用
返回[NSString stringWithFormat:
@%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x“,
结果[0],结果[1],结果[2],结果[3],
结果[4],结果[5],结果[6],结果[7],
结果[8],结果[9],结果[10],结果[11],
结果[12],结果[13],结果[14],结果[15]
];
}
@实现Hello_MD5ViewController
@综合MD5文本;
-(iAction)按钮按下:(id)发件人{
NSString*input=[[NSString alloc]initWithString:plainText.text];
NSString*digest=md5(输入);//如果使用textfield.text传递字符串,结果将是错误的
//NSString*digest=md5(@“123”);//如果我在这样的代码中给出一个字符串,它将返回正确的结果
NSString*md5Result=[[NSString alloc]initWithFormat:
@“MD5结果\n%@”,摘要];
md5Text.text=md5Result;
//计算MD5值
}
-(无效)解除锁定
{
[明文发布];
[MD5文本发布];
[super dealoc];
}
-(无效)未收到记忆警告
{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
#pragma标记-视图生命周期
/*
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad
{
[超级视图下载];
}
*/
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
self.md5Text=nil;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
@结束
谢谢你的帮助
====更新版本@GMT+1 0251小时05/21/2011=======
您好\u MD5ViewController.h
//
// Hello_MD5ViewController.h
// Hello-MD5
//
//
#import <UIKit/UIKit.h>
@interface Hello_MD5ViewController : UIViewController {
UILabel *md5Text;
UITextField *plainText;
}
@property (nonatomic, retain) IBOutlet UILabel *md5Text;
- (IBAction)buttonPressed: (id)sender;
@end
// Hello-MD5
//
//
#import <UIKit/UIKit.h>
/*@interface NSString (md5Extension)
- (NSString *) md5;
@end
@interface NSData (md5Extension)
- (NSString *) md5;
@end
*/
@interface Hello_MD5ViewController : UIViewController {
UILabel *md5Text;
UITextField *plainText;
}
//@property (nonatomic, retain) NSString *input;
@property (nonatomic, retain) IBOutlet UILabel *md5Text;
- (IBAction)buttonPressed: (id)sender;
@property (nonatomic, retain) IBOutlet UITextField *plaintext;
@end
//Hello-MD5
//
//
#进口
/*@接口NSString(md5Extension)
-(NSString*)md5;
@结束
@接口NSData(md5Extension)
-(NSString*)md5;
@结束
*/
@接口Hello_MD5ViewController:UIViewController{
UILabel*md5Text;
UITextField*纯文本;
}
//@属性(非原子,保留)NSString*输入;
@属性(非原子,保留)IBUILabel*md5Text;
-(iAction)按钮按下:(id)发送者;
@属性(非原子,保留)IBOutlet UITextField*纯文本;
@结束
您好\u MD5ViewController.m
//
// Hello_MD5ViewController.m
// Hello-MD5
//
#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 call
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 = [[NSString alloc] initWithString: plainText.text];
NSString *digest = md5(input); //if I use textfield.text to passing the string, the result will be wrong
//NSString *digest = md5(@"123"); //if I give a string within code like this, it'll return correct result
NSString *md5Result = [[NSString alloc] initWithFormat:
@"MD5 RESULT \n%@", digest];
md5Text.text = md5Result;
//Calculate MD5 value
}
- (void)dealloc
{
[plainText release];
[md5Text 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
// Hello-MD5
#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 call
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;
@synthesize plaintext;
- (IBAction)buttonPressed:(id)sender {
//NSString *input = [[NSString alloc] initWithString: plainText.text];
if(plainText.text == nil)
{
NSLog(@"Disconnected.");
}
//NSLog(@"Output %@",plainText.text);
//NSString *digest = md5(input);
//NSString *digest = md5(@"123");
//NSString *md5Result = [[NSString alloc] initWithFormat:
// @"MD5 RESULT \n%@", digest];
//md5Text.text = md5Result;
//Calculate MD5 value
}
- (void)dealloc
{
[plainText release];
[md5Text release];
//[input 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
//Hello-MD5
#导入“Hello_MD5ViewController.h”
#导入//导入用于CC_MD5访问
NSString*md5(NSString*str)
{
常量字符*cStr=[str UTF8String];
无符号字符结果[16];
CC_MD5(cStr,strlen(cStr),result);//这是MD5调用
返回[NSString stringWithFormat:
@%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x“,
结果[0],结果[1],结果[2],结果[3],
结果[4],结果[5],结果[6],结果[7],
结果[8],结果[9],结果[10],结果[11],
结果[12],结果[13],结果[14],结果[15]
];
}
@实现Hello_MD5ViewController
@综合MD5文本;
@合成纯文本;
-(iAction)按钮按下:(id)发件人{
//NSString*input=[[NSString alloc]initWithString:plainText.text];
if(明文=nil)
{
NSLog(@“断开”);
}
//NSLog(@“输出%@”,明文.text);
//NSString*摘要=md5(输入);
//NSString*digest=md5(@“123”);
//NSString*md5Result=[[NSString alloc]initWithFormat:
//@“MD5结果\n%@”,摘要];
//md5Text.text=md5Result;
//计算MD5值
}
-(无效)解除锁定
{
[明文发布];
[MD5文本发布];
//[输入释放];
[super dealoc];
}
-(无效)未收到记忆警告
{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
#pragma标记-视图生命周期
/*
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad
{
[超级视图下载];
}
*/
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
self.md5Text=nil;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
@结束
您尚未在纯文本
上定义IBOutlet
。看起来好像没有连接。您正在请求文本