[英]Convert.ToString() to binary format not working as expected
int i = 20;
string output = Convert.ToString(i, 2); // Base2 formatting
i = -20;
output = Convert.ToString(i, 2);
Value Expected Actual 20 00000000000000000000000000010100 10100 -20 10000000000000000000000000010100 11111111111111111111111111101100
我可以看到,也許 20 的二進制 output 已被截斷,但我不明白 -20 的 output。 我的期望基於 base2 符號加上一個信念,即 integer 的有符號元素用最左邊的第一個數字表示。 0 表示正,1 表示負。 有人可以解釋結果,特別是-20的結果嗎?
.NET 中的負數以二進制表示為二進制補碼。
來自 MSDN - Convert.ToString 方法(Int32,Int32) :
如果 value 為負且 toBase 為 2、8 或 16,則返回的字符串使用二進制補碼表示
對於 integer -20 為 11111111111111111111111111101100 系統默認使用 2 的補碼。 1 的補碼不適合計算。
(2 的補碼是所有位加一的反轉)
在 2 的補碼中,這將使 20 + (-20) = 0,可以輕松計算數學,而無需擔心正負。
例如,在有符號字符中:15 = 00001111,-18 = (00010010) 的 2 補碼 = 11101101 + 1 = 11101110
00001111 +11101110 =11111101
由於第一位是 1,我們知道它是一個負值。 讓我們做一個反向 2 的補碼。
11111101 - 1 = 11111100 => -(00000011) 它給出 -3 其中 15 + (-18) = -3
int value = 31;
string binary = Convert.ToString(value, 2);
Console.WriteLine(binary.PadLeft(8, '0')); // Outputs "00011111"
這與其他所有人發布的答案基本相同,只是打包在一個方法中。
/// <summary>
/// Method to convert an integer to a string containing the number in binary. A negative
/// number will be formatted as a 32-character binary number in two's compliment.
/// </summary>
/// <param name="theNumber">self-explanatory</param>
/// <param name="minimumDigits">if binary number contains fewer characters leading zeros are added</param>
/// <returns>string as described above</returns>
public static string IntegerToBinaryString(int theNumber, int minimumDigits)
{
return Convert.ToString(theNumber, 2).PadLeft(minimumDigits, '0');
}
這個 function 完全符合您的要求
string ConvertToFuzzyFrogBinary(int input)
{
int prefix = (input).ToString("d8").Length-(Math.Abs((input))).ToString("d8").Length;
string binary_num = "00000000000000000000000000000000".Substring(0,32-Convert.ToString(Math.Abs(input),2).Length)+Convert.ToString(Math.Abs(input),2);
return "1".Substring(0,prefix)+binary_num.Substring(prefix,32-prefix);
}
這是我寫的一個例程,它可以滿足原始提問者的要求。 僅僅晚了5年!
/// <summary>Convert a number into a string of bits</summary>
/// <param name="value">Value to convert</param>
/// <param name="minBits">Minimum number of bits, usually a multiple of 4</param>
/// <exception cref="InvalidCastException">Value must be convertible to long</exception>
/// <exception cref="OverflowException">Value must be convertible to long</exception>
/// <returns></returns>
public static string ShowBits<T>(this T value, int minBits)
{
long x = Convert.ToInt64(value);
string retVal = Convert.ToString(x, 2);
if (retVal.Length > minBits) retVal = Regex.Replace(retVal, @"^1+", "1"); // Replace leading 1s with a single 1 - can pad as needed below
if (retVal.Length < minBits) retVal = new string(x < 0 ? '1' : '0', minBits - retVal.Length) + retVal; // Pad on left with 0/1 as appropriate
return retVal;
}
如果你想擺脫這種“影響” - 使用Math.Abs()
方法來獲取沒有符號的數值,
string output = Convert.ToString(Math.Abs(i), 2); // Base2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.