Come posso convertire uint in int in C #?
uintin a longcome longpuò contenere tutti i uintvalori dove come intnon può ( come già accennato )
Come posso convertire uint in int in C #?
uintin a longcome longpuò contenere tutti i uintvalori dove come intnon può ( come già accennato )
Risposte:
Dato:
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
--MODIFICARE
Informazioni incluse come menzionato da Kenan EK
uintè maggiore di int.MaxValueotterrai un risultato negativo se usi un cast, o un'eccezione se usi Convert.ToInt32.
Prendi nota delle parole chiave checkede unchecked.
È importante se vuoi che il risultato venga troncato all'int o un'eccezione sollevata se il risultato non si adatta a 32 bit con segno. L'impostazione predefinita è deselezionata.
Supponendo che tu voglia semplicemente sollevare i 32 bit da un tipo e scaricarli così come sono nell'altro tipo:
uint asUint = unchecked((uint)myInt);
int asInt = unchecked((int)myUint);
Il tipo di destinazione sceglierà alla cieca i 32 bit e li reinterpreterà.
Al contrario, se sei più interessato a mantenere i valori decimali / numerici all'interno dell'intervallo del tipo di destinazione stesso:
uint asUint = checked((uint)myInt);
int asInt = checked((int)myUint);
In questo caso, otterrai eccezioni di overflow se:
Nel nostro caso, volevamo che la uncheckedsoluzione conservasse i 32 bit così come sono, quindi ecco alcuni esempi:
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()prende uintcome valore.
uint i = 10;
int j = (int)i;
o
int k = Convert.ToInt32(i)
int intNumber = (int)uintNumber;
A seconda del tipo di valori che ti aspetti, potresti voler controllare quanto è grande uintNumber prima di eseguire la conversione. Un int ha un valore massimo di circa 0,5 uint.