views:

1536

answers:

4

Hello,

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.

+4  A: 

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;
Matt Olenik
Isn't this possible to overflow with big negatives? You can use `unchecked` to truncate instead: http://msdn.microsoft.com/en-us/library/a569z7k8(VS.71).aspx
Josh Stodola
+1  A: 

To convert a long to a ulong, simply cast it:

long a;
ulong b = (ulong)a;

C# will throw an exception if it is a negative number.

Chris
I don't beleive that this throws an exception with a negative value, but will not be what you expect... don't have .net on here (laptop), or would test it.
Tracker1
A: 

Try:

Convert.ToUInt32()
SirDemon
+1  A: 
Int32 i = 17;
UInt32 j = (UInt32)i;

EDIT: question is unclear whether you have a long or an int?

Mitch Wheat
I think John is referring to "old" longs in C or C++ that were 32 bits wide.
Matt Olenik
question says c#?
Mitch Wheat
I meant that he referred to it out of habit.
Matt Olenik