Home >Backend Development >C#.Net Tutorial >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; }</int></int>
##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 <div class="cnblogs_code"></div><p><strong> <span style="font-size: 14pt"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/001/053e340232d19ddde4b2cb7c71ab5415-1.png?x-oss-process=image/resize,p_40" class="lazy" alt=""></span></strong></p> <p></p><p><strong>3. Built-in conversion method<span style="max-width:90%"></span></strong></p> C#.Net’s built-in base conversion method:<p></p><pre class="brush:php;toolbar:false"> 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
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 <em></em>
[]
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!