English 中文(简体)
C#中的等效 j
原标题:Equivalent java cast in C#
  • 时间:2012-04-27 20:20:34
  •  标签:
  • c#

我需要帮助迁移一种 j法:

(int)System.currentTimeMillis(); //result -186983989 (java) return diferent values

But in C# return alway the same value:

DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan span = DateTime.UtcNow - Jan1st1970;
Int32 resultado = (int)span.TotalMilliseconds; //result is always -2147483648 and i need same as java

短视中很少见到显示正确价值,但在执行中则见<代码>resultado<>/code>。 a)

I need this -186983989 result, same as java.

问题回答

你希望使用Int64(一)。 Int32 isn t amount to hold the Value.

Int32.MaxValue = 2,147,483,647
Int64.MaxValue = 9,223,372,036,854,775,808

http://forums.asp.net/post/1203789.aspx”rel=“nofollow>http://forums.asp.net/post/1203789.aspx

总数为两倍,而不是印第32号。 问题

To mimic the overflow behavior you could do somthing using the unchecked keyword but I don t know enough about Java to know if this correct

DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan span = DateTime.UtcNow - Jan1st1970;

unchecked{
     int mm = Int32.MaxValue;
     mm += (int)(span.TotalMilliseconds % Int32.MaxValue);
     Console.WriteLine(mm); 
}

页: 1





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签