HomeCSharpHow to convert Binary Number to Decimal , HexaDecimal and Octal Number using C# ?

How to convert Binary Number to Decimal , HexaDecimal and Octal Number using C# ?

This is my second post of the series of the Number Converter for Windows Phone 7 sourcecode . In my previous post i talked about

In this blog post , i describe or share the code  that converts a Binary Number to Decimal , HexaDecimal and Octal Number using C# in Windows Phone 7 .

public static class BinaryConverter
{
   /// <summary>
   /// Binaries to decimal.
   /// </summary>
   /// <param name="bin">The bin.</param>
   /// <returns></returns>
   public static Int64 BinaryToDecimal(string bin)
   {
      long l = Convert.ToInt64(bin, 2);
      int i = (int)l;
      return i;
   }
   /// <summary>
   /// Binaries to octal.
   /// </summary>
   /// <param name="bin">The bin.</param>
   /// <returns></returns>
   public static Int64 BinaryToOctal(string bin)
   {
      Int64 Deci = BinaryConverter.BinaryToDecimal(bin);
      return DecimalConverter.DecimalToOctal(Deci);
   }
   /// <summary>
   /// Binaries to hexa.
   /// </summary>
   /// <param name="bin">The bin.</param>
   /// <returns></returns>
   public static string BinaryToHexa(string bin)
   {
      Int64 Deci = BinaryConverter.BinaryToDecimal(bin);
      return DecimalConverter.DecimalToHex(Deci);
   }
}

DecimalConverter is a static class that was described in my previous blog post . The Convert method is again used to convert from Binary to decimal .

Please do share with me of any other simple logic to convert Binary Number to other formats 🙂

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...