How do I convert uint to int in C#?
8 Answers
Given:
uint n = 3;
int i = checked((int)n); //throws OverflowException if n > Int32.MaxValue
int i = unchecked((int)n); //converts the bits only
//i will be negative if n > Int32.MaxValue
int i = (int)n; //same behavior as unchecked
or
int i = Convert.ToInt32(n); //same behavior as checked
--EDIT
Included info as mentioned by Kenan E. K.
-
4Note that if the
uint
is greater thanint.MaxValue
you'll get a negative result if you use a cast, or an exception if you useConvert.ToInt32
.– LukeHJul 15, 2009 at 14:52 -
7
-
7@Luke - No, not necessarily. When casting it depends on your project build settings as to whether checked or unchecked arithmentic is the default. In addition, you can modify this on a local basis using the checked and unchecked keywords. Jul 15, 2009 at 14:59
-
@Greg: That's true, but the default out-of-the-box setting is unchecked.– LukeHJul 15, 2009 at 15:03
-
@GregBeech Is this behaviour portable ? in case of unchecked approach ??– user5528169Dec 10, 2015 at 11:57
Assuming you want to simply lift the 32bits from one type and dump them as-is into the other type:
uint asUint = unchecked((uint)myInt);
int asInt = unchecked((int)myUint);
The destination type will blindly pick the 32 bits and reinterpret them.
Conversely if you're more interested in keeping the decimal/numerical values within the range of the destination type itself:
uint asUint = checked((uint)myInt);
int asInt = checked((int)myUint);
In this case, you'll get overflow exceptions if:
- casting a negative int (eg: -1) to an uint
- casting a positive uint between 2,147,483,648 and 4,294,967,295 to an int
In our case, we wanted the unchecked
solution to preserve the 32bits as-is, so here are some examples:
Examples
int => uint
int....: 0000000000 (00-00-00-00)
asUint.: 0000000000 (00-00-00-00)
------------------------------
int....: 0000000001 (01-00-00-00)
asUint.: 0000000001 (01-00-00-00)
------------------------------
int....: -0000000001 (FF-FF-FF-FF)
asUint.: 4294967295 (FF-FF-FF-FF)
------------------------------
int....: 2147483647 (FF-FF-FF-7F)
asUint.: 2147483647 (FF-FF-FF-7F)
------------------------------
int....: -2147483648 (00-00-00-80)
asUint.: 2147483648 (00-00-00-80)
uint => int
uint...: 0000000000 (00-00-00-00)
asInt..: 0000000000 (00-00-00-00)
------------------------------
uint...: 0000000001 (01-00-00-00)
asInt..: 0000000001 (01-00-00-00)
------------------------------
uint...: 2147483647 (FF-FF-FF-7F)
asInt..: 2147483647 (FF-FF-FF-7F)
------------------------------
uint...: 4294967295 (FF-FF-FF-FF)
asInt..: -0000000001 (FF-FF-FF-FF)
------------------------------
Code
int[] testInts = { 0, 1, -1, int.MaxValue, int.MinValue };
uint[] testUints = { uint.MinValue, 1, uint.MaxValue / 2, uint.MaxValue };
foreach (var Int in testInts)
{
uint asUint = unchecked((uint)Int);
Console.WriteLine("int....: {0:D10} ({1})", Int, BitConverter.ToString(BitConverter.GetBytes(Int)));
Console.WriteLine("asUint.: {0:D10} ({1})", asUint, BitConverter.ToString(BitConverter.GetBytes(asUint)));
Console.WriteLine(new string('-',30));
}
Console.WriteLine(new string('=', 30));
foreach (var Uint in testUints)
{
int asInt = unchecked((int)Uint);
Console.WriteLine("uint...: {0:D10} ({1})", Uint, BitConverter.ToString(BitConverter.GetBytes(Uint)));
Console.WriteLine("asInt..: {0:D10} ({1})", asInt, BitConverter.ToString(BitConverter.GetBytes(asInt)));
Console.WriteLine(new string('-', 30));
}
Take note of the checked
and unchecked
keywords.
It matters if you want the result truncated to the int or an exception raised if the result doesnt fit in signed 32 bits. The default is unchecked.
-
2I believe the default is unchecked. +1 for pointing this out--the source of hard to debug errors.– user1228Jul 15, 2009 at 14:51
-
You are right, my bad, thanks! -"The default value for this option is /checked-" Jul 15, 2009 at 14:55
-
1
Assuming that the value contained in the uint can be represented in an int, then it is as simple as:
int val = (int) uval;
I would say using tryParse, it'll return 'false' if the uint is to big for an int.
Don't forget that a uint can go much bigger than a int, as long as you going > 0
int intNumber = (int)uintNumber;
Depending on what kind of values you are expecting, you may want to check how big uintNumber is before doing the conversion. An int has a max value of about .5 of a uint.
uint
to along
as along
can contain alluint
values where as anint
can't (as already mentioned)