Home > Article > Backend Development > Detailed explanation of decimal to binary, hexadecimal and octal conversion in C#
1. Convert decimal to binary
Continuously divide the decimal number by 2 and fill in all the remainders backwards to get the required binary data.
public static string DecimalToBinary(int vDecimal) {/* 将十进制的数 vDecimal 不断地除 2,取余数 * 然后将余数 倒序 填写 */List<int> vYuShu = new List<int>(); // 除 2 过程中产生的余数集int vTempValue= vDecimal; // 除 2 过程中产生的商数for (; ; ) {int tempYS = vTempValue % 2; vYuShu.Add(tempYS); // 记住余数vTempValue = vTempValue / 2;if (vTempValue == 0) // 商数等于0时,结束运算break; }// 倒序输出string strBinary = "";for (int i = vYuShu.Count - 1; i >= 0; i--) { strBinary += vYuShu[i]; } Console.WriteLine("Input decimal value:{0}, output binary value:{1}.", vDecimal, strBinary);return strBinary; }
##2. Convert binary to decimal
Multiply the value (0 or 1) in each binary digit by 2 raised to the (n-1) power, and convert each bit The results are added. Among them, n represents the number of digits from right to left in binary (counting from 1);public static int BinaryToDecimal(string vBinary) {// 首先判断是否满足输入要求int[] vInput = new int[vBinary.Length];for (int i = 0; i < vBinary.Length; i++) {var tempNum = vBinary[i].ToString();if (tempNum == "0") { vInput[i] = 0; }else if (tempNum == "1") { vInput[i] = 1; }else{throw new Exception("输入参数不正确,二进制数应仅由:0和1组成"); } }/* * 依次乘2的(n-1)次方,再求和 */int vDecimal = 0;for (int i = 1; i <= vInput.Length; i++) { vDecimal += (int)(Math.Pow(2, i - 1) * vInput[vInput.Length-i]); } Console.WriteLine("Input binary value:{0}, output decimal value:{1}.", vBinary, vDecimal);return vDecimal; }
3. Built-in conversion method
C#.Net’s built-in base conversion method:int vDecimal = 99;// 【10】 → 【2】string vBinary = Convert.ToString(vDecimal, 2); Console.WriteLine("十进制数:{0},转换成二进制:{1}", vDecimal, vBinary);// 【2】 → 【10】int tempDecimal = Convert.ToInt32(vBinary, 2); Console.WriteLine("二进制数:{0},转换成十进制:{1}", vBinary, tempDecimal);
#4. Decimal <=> Hexadecimal
int vDecimal = 127;// 【10】 → 【16】string vStrHex = "0x" + Convert.ToString(vDecimal, 16); Console.WriteLine("十进制数:{0},转换成十六进制:{1}", vDecimal, vStrHex);// 【16】 → 【10】int tempDecimal = Convert.ToInt32(vStrHex, 16); Console.WriteLine("十六进制数:{0},转换成十进制:{1}", vStrHex, tempDecimal);
## Or you can:
##5. Decimal<=> Octal
##6. Other conversions
7. Signed number binary conversion
For data with positive and negative signs, the conversion is slightly different from the appeal.
1 byte (8 bits) can only represent 256 numbers anyway. Because it is signed, we express it as a range: -128 → 127.Use the highest bit to represent the sign bit, 0 represents a positive number, and 1 represents a negative number.
10000000 represents the smallest negative integer in the computer. From 10000001 to 11111111, it means -127 to -1.
Negative integers are stored in two's complement form in computers.
public static int BinaryToDecimalWithSign(string vBinary)
{// 首先判断是否满足输入要求int[] vInput = new int[vBinary.Length];for (int i = 0; i < vBinary.Length; i++)
{var tempNum = vBinary[i].ToString();if (tempNum == "0")
{
vInput[i] = 0;
}else if (tempNum == "1")
{
vInput[i] = 1;
}else{throw new Exception("输入参数不正确,二进制数应仅由:0和1组成");
}
}// -------- 不足8bits,补足 --------(非必需)if (vInput.Length % 8 != 0) // 补足8b、16b、、、 {int nLen = (vInput.Length / 8 + 1) * 8;int[] nInput = new int[nLen];for (int i = 0; i < nLen - vInput.Length; i++)
{
nInput[i] = vInput[0];
}
vInput.CopyTo(nInput, nLen - vInput.Length);
vInput = nInput;
}// ---------------------------------// 第1步:首位为1,则为负值int vFH = vInput[0];if (vFH == 1)
{// ---------- 第2步:减去一 ----------for (int i = 1; i <= vInput.Length; i++)
{if (vInput[vInput.Length - i] == 1)
{
vInput[vInput.Length - i] = 0;break;
}else{
vInput[vInput.Length - i] = 1;
}
}// ---------- 第3步:取反 ----------for (int i = 0; i < vInput.Length; i++)
{
vInput[i] = 1 - vInput[i];
}
}// ---------- 第4步:转成10进制数 ----------int vDecimal = 0;for (int i = 1; i <= vInput.Length; i++)
{
vDecimal += (int)(Math.Pow(2, i - 1) * vInput[vInput.Length - i]);
}if (vFH == 1) // 为负数 {
vDecimal = 0 - vDecimal;
}
Console.WriteLine("Input binary value:{0}, output decimal value:{1}.", vBinary, vDecimal);return vDecimal;
}
[]
The above is the detailed content of Detailed explanation of decimal to binary, hexadecimal and octal conversion in C#. For more information, please follow other related articles on the PHP Chinese website!