Convert a decimal to the binary format : Convert « Data Types « C# / C Sharp






Convert a decimal to the binary format

    
//GNU General Public License version 2 (GPLv2)
//http://cbasetest.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SDFL.Helper
{
    public class StrHelper
    {
        /// <summary>
        ///         Convert a decimal to the binary format.
        ///         like: 
        ///         stra = Convert.ToString(4, 2).PadLeft(8, '0');
        ///         //stra=00000100
        /// </summary>
        /// <param name="DecimalNum"></param>
        /// <returns></returns>
        public static string ToBinary(Int64 decimalNum)
        {
            Int64 binaryHolder;
            char[] binaryArray;
            string binaryResult = "";

            // fix issue#5943: StrHelper.ToBinary(0)  result "", it should be 0
            if (decimalNum == 0)
            {
                return "0";
            }
            // end fix issue#5943

            while (decimalNum > 0)
            {
                binaryHolder = decimalNum % 2;
                binaryResult += binaryHolder;
                decimalNum = decimalNum / 2;
            }

            // rever the binaryResult, e.g. we get 1101111, then revert to 1111011, this is the final result
            binaryArray = binaryResult.ToCharArray();
            Array.Reverse(binaryArray);
            binaryResult = new string(binaryArray);
            return binaryResult;
        }    

    }
}

    

   
    
    
    
  








Related examples in the same category

1.Convert string to int
2.Hidden among the To(integral-type) methods are overloads that parse numbers in another base:
3.The source and target types must be one of the "base" types. ChangeType also accepts an optional IFormatProvider argument. Here's an example:
4.Convert string to byte array with for loop
5.Convert byte array to string
6.Convert.ToString Converts the value of a 32-bit signed integer to its equivalent string representation in a specified base.
7.Convert double to bool
8.Convert double to string
9.Convert console input to double
10.Converts int to string in a specified base.
11.Convert double to int
12.Convert double to bool and string
13.Convert string to char
14.Convert what you read from console into int
15.Convert various data types to integer 64
16.Returns an object of the specified type and whose value is equivalent to the specified object.
17.Convert.ChangeType Method (Object, TypeCode, IFormatProvider)
18.Converts a Unicode character array to an 8-bit unsigned integer array.
19.Convert.ToBase64CharArray() and Convert.FromBase64CharArray
20.Converts byte to Boolean
21.Converts decimal to Boolean value.
22.Converts double to Boolean value.
23.Converts short to Boolean value.
24.Converts int to Boolean value.
25.Converts long to Boolean value.
26.Converts string to Boolean
27.Converts Boolean value to byte.
28.Converts Unicode character to byte
29.Converts decimal number to byte
30.Converts double-precision floating-point number to byte
31.Convert string to bool with exception catch
32.Converts a string of length 4 to a 32-bit unsigned integer.
33.Converts a 32-bit unsigned integer to a string of length 4.
34.Conversion between C# string and packed string representation.
35.Convert object value to double or int