C#: How to convert long to ulong

Ivan Prodanov picture Ivan Prodanov · Mar 27, 2009 · Viewed 31k times · Source

If i try with BitConverter,it requires a byte array and i don't have that.I have a Int32 and i want to convert it to UInt32.

In C++ there was no problem with that.

Answer

Matthew Olenik picture Matthew Olenik · Mar 27, 2009

A simple cast is all you need. Since it's possible to lose precision doing this, the conversion is explicit.

long x = 10;
ulong y = (ulong)x;