한 유형에서 32 비트를 간단히 들어 올려 다른 유형에 그대로 덤프하려고한다고 가정합니다.
uint asUint = unchecked((uint)myInt);
int asInt = unchecked((int)myUint);
대상 유형은 맹목적으로 32 비트를 선택하여 재 해석합니다.
반대로 대상 유형 자체의 범위 내에서 십진수 / 숫자 값을 유지하는 데 더 관심이있는 경우 :
uint asUint = checked((uint)myInt);
int asInt = checked((int)myUint);
이 경우 다음과 같은 경우 오버플로 예외가 발생합니다.
- 음의 정수 (예 : -1)를 uint로 캐스팅
- 2,147,483,648에서 4,294,967,295 사이의 양의 단위를 int로 캐스팅
우리의 경우 unchecked32 비트를 그대로 유지 하는 솔루션을 원했기 때문에 다음과 같은 몇 가지 예가 있습니다.
예
int => 단위
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 => 정수
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));
}