C # decimal and binary, 16 binary, octal conversion

Source: Internet
Author: User
Tags pow

1. Decimal Turn Binary









The decimal number is continuously removed by 2, and all the remainder is written in flashbacks to obtain the required binary data.




public static string DecimalToBinary (int vDecimal)

        {/ * Divide the decimal number vDecimal continuously by 2 and take the remainder

             * Then fill the remainder in reverse order * / List <int> vYuShu = new List <int> (); // Set of remainders generated during division 2 int vTempValue = vDecimal; // Quotient generated during division 2 for (; ;)

            {int tempYS = vTempValue% 2;

                vYuShu.Add (tempYS); // Remember the remainder vTempValue = vTempValue / 2; if (vTempValue == 0) // End the operation when the quotient is equal to 0;

            } // Reverse output 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. Binary Turn Decimal






Multiply the values on the binary (0 or 1) by 2 (n-1), adding each result. where n represents the right-to-left number of bits in the binary (starting from 1);




public static int BinaryToDecimal (string vBinary)

        {// First determine whether the input requirements are met 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 ("The input parameters are incorrect. The binary number should only consist of: 0 and 1");

                }

            } / *

             * Multiply (n-1) th power of 2 and sum up * / 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. Self-bringing conversion mode






C#. NET self-made conversion mode:




int vDecimal = 99; // [10] → [2] string vBinary = Convert.ToString (vDecimal, 2);

            Console.WriteLine ("Decimal: {0}, converted to binary: {1}", vDecimal, vBinary); // [2] → [10] int tempDecimal = Convert.ToInt32 (vBinary, 2);

            Console.WriteLine ("Binary number: {0}, converted to decimal: {1}", vBinary, tempDecimal);










4. Decimal <=> hexadecimal




int vDecimal = 127; // [10] → [16] string vStrHex = "0x" + Convert.ToString (vDecimal, 16);

            Console.WriteLine ("Decimal number: {0}, converted to hexadecimal: {1}", vDecimal, vStrHex); // [16] → [10] int tempDecimal = Convert.ToInt32 (vStrHex, 16);

            Console.WriteLine ("Hexadecimal: {0}, converted to decimal: {1}", vStrHex, tempDecimal);










Or you can:












5. Decimal <=> octal












6. Other conversions


















7. Signed Number binary conversions






For the signed data, the conversion is slightly different from the appeal.






1 bytes (8 bits) it can only represent 256 numbers anyway, because there is a sign, so we're going to show it as a range: -128→127.






The symbol bit is represented by the highest position, 0 is a positive number, and 1 indicates a negative number.






10000000 represents the smallest negative integer in the computer. From 10000001 to 11111111 in turn-127 to-1.






Negative integers are stored in complementary form in the computer.




public static int BinaryToDecimalWithSign (string vBinary)

       {// First determine whether the input requirements are met 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 ("The input parameters are incorrect. The binary number should only consist of: 0 and 1");

               }

           } // -------- less than 8bits, make up -------- (optional) if (vInput.Length% 8! = 0) // make up 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;

           } // --------------------------------- // Step 1: If the first bit is 1, it is negative int vFH = vInput [0]; if (vFH == 1)

           {// ---------- Step 2: Subtract one ---------- 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;

                   }

               } // ---------- Step 3: Invert ---------- for (int i = 0; i <vInput.Length; i ++)

               {

                   vInput [i] = 1-vInput [i];

               }

           } // ---------- Step 4: Convert to decimal number ---------- 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) // is negative {

               vDecimal = 0-vDecimal;

           }


           Console.WriteLine ("Input binary value: {0}, output decimal value: {1}.", VBinary, vDecimal); return vDecimal;

       }
















Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.