C#日期类型转换与c ++相同

Tea*_*mol 4 c# c++ native ulong long-integer

如何将ulong转换为long,结果与我在c ++中得到的结果相同.

C++

unsigned long ul = 3633091313;
long l = (long)ul;
l is -661875983
Run Code Online (Sandbox Code Playgroud)

C#

ulong ul = 3633091313;
long l = (long)ul;
l is 3633091313
Run Code Online (Sandbox Code Playgroud)

das*_*ght 8

C++ long通常是32位数据类型.它看起来像在你的系统中没有足够的位来表示3633091313,所以结果是负的.在C++(相关问答)中未定义此行为.

在C#中,对应于转换为int:

UInt64 ul = 3633091313UL;
Int32 l = (int)ul;
Console.WriteLine(l); // prints -661875983
Run Code Online (Sandbox Code Playgroud)

演示.