Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing input value of Textfield to calculate MD5
    text
    copied!<p>I met a problem while trying to passing the inputed strings of Textfield to calculate MD5.</p> <p>When I just give a specified string, like abc, in my code, and do the MD5 calculation, it will return a correct result.</p> <p>Things went wrong when I try to using a textfield to let the user input the same string, abc, and then I pass the textfield.text to md5 function to do the md5 hash. This time, the result was different.</p> <p>I am totally confused with this problem and have been got stuck at there for nearly a week, but just couldn't figure out why and how to solve it.</p> <p>Could you please help me with that?</p> <p>here's my code:</p> <p>Hello_MD5ViewController.h</p> <pre><code>// // Hello_MD5ViewController.h // Hello-MD5 // // #import &lt;UIKit/UIKit.h&gt; @interface Hello_MD5ViewController : UIViewController { UILabel *md5Text; UITextField *plainText; } @property (nonatomic, retain) IBOutlet UILabel *md5Text; - (IBAction)buttonPressed: (id)sender; @end </code></pre> <p>Hello_MD5ViewController.m</p> <pre><code>// // Hello_MD5ViewController.m // Hello-MD5 // #import "Hello_MD5ViewController.h" #import &lt;CommonCrypto/CommonDigest.h&gt; //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 </code></pre> <p>Thank you for your help!</p> <p>=====UPDATED VERSION @ GMT+1 0251 hrs 05/21/2011=======</p> <p>Hello_MD5ViewController.h</p> <pre><code>// Hello-MD5 // // #import &lt;UIKit/UIKit.h&gt; /*@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 </code></pre> <p>Hello_MD5ViewController.m</p> <pre><code>// Hello-MD5 #import "Hello_MD5ViewController.h" #import &lt;CommonCrypto/CommonDigest.h&gt; //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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload