How do I convert uint to int in C#?

前端 未结 8 1483
时光说笑
时光说笑 2020-12-05 03:54

How do I convert uint to int in C#?

相关标签:
8条回答
  • 2020-12-05 04:05

    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.

    0 讨论(0)
  • 2020-12-05 04:07

    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

    0 讨论(0)
  • 2020-12-05 04:13

    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));
    }  
    
    0 讨论(0)
  • 2020-12-05 04:14
    uint i = 10;
    int j = (int)i;
    

    or

    int k = Convert.ToInt32(i)
    
    0 讨论(0)
  • 2020-12-05 04:19

    Assuming that the value contained in the uint can be represented in an int, then it is as simple as:

    int val = (int) uval;

    0 讨论(0)
  • 2020-12-05 04:21

    Convert.ToInt32() takes uint as a value.

    0 讨论(0)
提交回复
热议问题