C # conversion between decimal and binary, hexadecimal, and octal,

Source: Internet
Author: User
Tags binary to decimal decimal to binary

C # conversion between decimal and binary, hexadecimal, and octal,

1. Convert decimal to binary

After dividing the decimal number by 2, you can enter all the remainder into two parts to obtain the required binary data.

Public static string DecimalToBinary (int vDecimal) {/* divide the decimal number vDecimal by 2, take the remainder * and enter the remainder in reverse order */List <int> vYuShu = new List <int> (); // The remainder set int vTempValue = vDecimal generated in the division of 2; // The quotient generated in the process of Division 2 for (;) {int tempYS = vTempValue % 2; vYuShu. add (tempYS); // remember the remainder vTempValue = vTempValue/2; if (vTempValue = 0) // when the number of businesses is equal to 0, terminate the break operation ;} // output string strBinary = "" in reverse order; 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 values (0 or 1) in binary values by the (n-1) power of 2 and add each result. Where, n indicates the number of digits from right to left in the binary (starting from 1 );

Public static int BinaryToDecimal (string vBinary) {// first, judge whether the input meets the requirement 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 parameter is incorrect, and the binary number should only consist of: 0 and 1 ");}} /** multiply the power (n-1) of 2 in sequence, and then sum */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 built-in hexadecimal conversion method:

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: {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: {0}, converted to hexadecimal: {1}", vDecimal, vStrHex); // [16] → [10] int tempDecimal =Convert. ToInt32 (vStrHex, 16); Console. WriteLine ("hexadecimal number: {0}, converted to decimal: {1}", vStrHex, tempDecimal );

Alternatively, you can:

5. Decimal <=> octal

6. Other Conversions

 

7. Binary Conversion of signed numbers

For positive and negative numbers, the conversion is slightly different from the appeal.

One byte (eight bits) can only represent 256 numbers in any case, because it is signed, so we will express it in the range:-128 → 127.

Use the highest bit to represent the symbol bit, 0 to represent a positive number, and 1 to represent a negative number.

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

Negative integers are stored in the computer as supplementary codes.

Public static int BinaryToDecimalWithSign (string vBinary) {// first, judge whether the input meets the requirement 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 parameter is incorrect, and the binary number should only consist of: 0 and 1 ");}} // -------- less than 8 bits, supplement -------- (not required) if (vInput. length % 8! = 0) // supplement 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 4: if the first part is 1, the negative value int vFH = vInput [0]; if (vFH = 1) {// ---------- Step 1: 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 1: reverse ---------- for (int I = 0; I <vInput. length; I ++) {vInput [I] = 1-vInput [I] ;}// ---------- Step 1: convert it to a 10-digit 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) // negative {vDecimal = 0-vDecimal;} Console. writeLine ("Input binary value: {0}, output decimal value: {1 }. ", vBinary, vDecimal); return vDecimal ;}

 

[Http://www.cnblogs.com/CUIT-DX037/]

 

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.