Convert.ToString() to binary format not working as expected

前端 未结 7 789
说谎
说谎 2021-01-17 10:33
int i = 20;
string output = Convert.ToString(i, 2); // Base2 formatting
i = -20;
output = Convert.ToString(i, 2);
Value   Expected                       


        
7条回答
  •  别那么骄傲
    2021-01-17 11:04

    Negative numbers in .NET are represented in binary as Two's complement.

    From MSDN - Convert.ToString Method (Int32, Int32):

    If value is negative and toBase is 2, 8, or 16, the returned string uses two's complement representation

提交回复
热议问题