¿Cómo convierto uint a int en C #?
uinten un longya que longpuede contener todos los uintvalores donde intno se puede ( como ya se mencionó )
¿Cómo convierto uint a int en C #?
uinten un longya que longpuede contener todos los uintvalores donde intno se puede ( como ya se mencionó )
Respuestas:
Dado:
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
o
int i = Convert.ToInt32(n); //same behavior as checked
--EDITAR
Información incluida mencionada por Kenan EK
uintes mayor que int.MaxValueobtendrá un resultado negativo si usa un yeso, o una excepción si usa Convert.ToInt32.
Tome nota de las palabras clave checkedy unchecked.
Importa si desea que el resultado se trunque al int o que se genere una excepción si el resultado no encaja en 32 bits con signo. El valor predeterminado no está marcado.
Suponiendo que simplemente desea levantar los 32 bits de un tipo y volcarlos tal cual en el otro tipo:
uint asUint = unchecked((uint)myInt);
int asInt = unchecked((int)myUint);
El tipo de destino elegirá ciegamente los 32 bits y los reinterpretará.
Por el contrario, si está más interesado en mantener los valores decimales / numéricos dentro del rango del tipo de destino en sí:
uint asUint = checked((uint)myInt);
int asInt = checked((int)myUint);
En este caso, obtendrá excepciones de desbordamiento si:
En nuestro caso, queríamos que la uncheckedsolución conservara los 32 bits tal cual, así que aquí hay algunos ejemplos:
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...: 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)
------------------------------
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));
}
Convert.ToInt32()toma uintcomo valor.
uint i = 10;
int j = (int)i;
o
int k = Convert.ToInt32(i)
int intNumber = (int)uintNumber;
Dependiendo del tipo de valores que esté esperando, es posible que desee verificar qué tan grande es uintNumber antes de realizar la conversión. Un int tiene un valor máximo de aproximadamente 0,5 de un uint.