views:

669

answers:

2

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

+4  A: 

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);
Charlie Martin
temp = [md5(password) copy] or [md5(password) retain] may be needed though
cobbal
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.
danielpunkass
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.
Charlie Martin
A: 

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
Pablo Santa Cruz