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

前端 未结 7 824
说谎
说谎 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:01

    int value = 31;
    string binary = Convert.ToString(value, 2);
    Console.WriteLine(binary.PadLeft(8, '0'));          // Outputs "00011111"
    

提交回复
热议问题