0

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.

  • 1
    What is the expected output for the date? – Davin Tryon Dec 14 '12 at 17:24
  • 1
    Doesn't `dataUsingEncoding:NSTimeInterval` throw error on your screen? – Inder Kumar Rathore Dec 14 '12 at 17:33
  • The `%@` formatting code causes an Objective-C object to be displayed. For NSStrings it just prints the string characters. The `%.0f` code is to format a floating-point number (in non-exponential format). The format indicates that the number is to be printed without any fractional part. – Hot Licks Dec 14 '12 at 17:49

3 Answers3

1
var authDataString = String.Format("{0}{1:F0}{2}",username, authTime, password);
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
1

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.

uranusjr
  • 1,380
  • 12
  • 36
  • Hey thanks for the quick answer, it was explained very well. :D I think I've got it figured out now with your and everyone's help. – user1535485 Dec 14 '12 at 17:55
0
  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.

Hvordan har du det
  • 3,605
  • 2
  • 25
  • 48