Como faço para converter uint em int em C #?
uintpara longcomo um longpode conter todos os uintvalores onde como um intnão pode ( como já mencionado )
Como faço para converter uint em int em C #?
uintpara longcomo um longpode conter todos os uintvalores onde como um intnão pode ( como já mencionado )
Respostas:
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
ou
int i = Convert.ToInt32(n); //same behavior as checked
--EDITAR
Informações incluídas conforme mencionado por Kenan EK
uintfor maior do que, int.MaxValuevocê obterá um resultado negativo se usar um elenco ou uma exceção se usar Convert.ToInt32.
Anote as palavras checked- uncheckedchave e .
É importante se você deseja que o resultado seja truncado para o int ou uma exceção levantada se o resultado não couber em 32 bits com sinal. O padrão é desmarcado.
Supondo que você deseja simplesmente retirar os 32 bits de um tipo e despejá-los como estão no outro tipo:
uint asUint = unchecked((uint)myInt);
int asInt = unchecked((int)myUint);
O tipo de destino escolherá cegamente os 32 bits e os reinterpretará.
Por outro lado, se você estiver mais interessado em manter os valores decimais / numéricos dentro do intervalo do próprio tipo de destino:
uint asUint = checked((uint)myInt);
int asInt = checked((int)myUint);
Nesse caso, você obterá exceções de estouro se:
Em nosso caso, queríamos que a uncheckedsolução preservasse os 32 bits como estão, então aqui estão alguns exemplos:
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()leva uintcomo um valor.
uint i = 10;
int j = (int)i;
ou
int k = Convert.ToInt32(i)
int intNumber = (int)uintNumber;
Dependendo dos tipos de valores que você está esperando, você pode verificar o tamanho de uintNumber antes de fazer a conversão. Um int tem um valor máximo de cerca de 0,5 de um uint.