1

I have a C function with the following method signature.

NSString* md5( NSString *str )

How do I call this function, pass in a string, and save the returned string?

I tried the following, but it did not work:

NSString *temp= [[NSString alloc]initWithString:md5(password)];

thanks for your help

2
  • That code looks fine, assuming password is an NSString variable. You're going to need to provide more details. How did it "not work"? Where does password come from? Commented Mar 17, 2009 at 2:20
  • May help to indicate what exactly didn't work. Did the compiler give an error? What was the error? What happens if you try NSString *a = md5(password); NSString *temp = [[NSString alloc] initWithString:a]; Commented Mar 17, 2009 at 2:21

2 Answers 2

5

You're making it too hard. The stuff in []'s is effectively smalltalk. What you want is to just call the function in C:

NSString * temp = md5(password);
Sign up to request clarification or add additional context in comments.

3 Comments

temp = [md5(password) copy] or [md5(password) retain] may be needed though
It's true they're making it too hard, but their code should work, as the comments on the question suggest. Question is bad and needs more details.
yeah, but "making it too hard" is usually a sign they don't understand what they're trying to do. Agree more details might help.
0

What is password? Is password a common "char *" pointer? Is the md5 signature you put correct?

If that's the case, you could:

NSString *temp = [[NSString alloc] initWithCString:password  encoding:NSASCIIStringEncoding];

If your md5 signature is: char *md5(char *password), and you have you password stored in a NSString, you could:

NSString password = @"mypass";
char buff[128];
NSString *temp = [[NSString alloc] initWithString:password];
[temp getCString:buff maxLength:128 encoding:NSASCIIStringEncoding];
char *md5 = md5(buff);
// then you could do whatever you want with md5 var

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.