Question

Okay, I am working on converting some objective c code to c# here is what I have.

NSTimeInterval now = [[NSDate date] timeIntervalSince1970];


NSData * formattedstring= [[NSString stringWithFormat:@"%@%.0f%@", string1, now, string2] dataUsingEncoding:NSUTF8StringEncoding];

So for I have created a helper class and I have this:

var authTime = GetTimeIntervalSince1970(DateTime.Now);

var authDataString = String.Format("{0}{1}{2]",username, authTime, password);

So, my question is the "%.0f" and the "dataUsingEncoding:NSTimeInterval". I know the first part has something to do with formatting the "now" parameter, what do I need to do to make sure I'm doing the same thing in c#, and can someone explain this to me in detail or direct me to an article/blog I should read?

Thanks!

Update: Okay guys I messed up, I'm sorry I copied and pasted wrong which is party of my confusion. the dataUsingEncoding:NSTimeInterval should read: dataUsingEncoding:NSUTF8StringEncoding. So I have fixed the post.

Was it helpful?

Solution

The second line of your Objective-C code consists of two parts:

NSString *tmp = [NSString stringWithFormat:@"%@%.0f%@", string1, now, string2];

and

NSData *formattedstring = [tmp dataUsingEncoding:NSTimeInterval];

The first part generates a string. NSTimeInterval is typedefed to double, so %.0f basically format the floor of now (e.g. from 3.1415926 to @"3"). So, assuming your GetTimeIntervalSince1970 returns a floating number, the equivalent in C# is

string tmp = string.Format("{0}{1:F0}{2}", username, authTime, password);

The second part, however, is confusing. dataUsingEncoding: takes an NSStringEncoding argument, but NSTimeInterval is not one of the available built-in constants. As a matter of fact, this most likely shouldn't even compile because NSTimeInterval is an typedef, and can't be converted (implicitly) to an integer. I think NSData is roughly equivalent to System.Byte[] in C#, but whether you need to convert the string depends on your specific need.

OTHER TIPS

var authDataString = String.Format("{0}{1:F0}{2}",username, authTime, password);
  1. For the "%.0f", you can read Apple's documents (It's just ANSI C's printf format plus %@ for any object):

  2. As for dataUsingEncoding:NSTimeInterval,I don't think the original Objective-C code is correct. NSString's dataUsingEncoding: method's signature is like this:

    - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding

    Possible values for encoding are defined here: String Encodings, passing NSTimeInterval as NSStringEncoding makes no sense here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top