Format integer as octal, binary and hexadecimal numbers in CSharp

Description

The following code shows how to format integer as octal, binary and hexadecimal numbers.

Example


//  w  w w  . j a  va  2  s . c o  m
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {

      int[] numbers = { -1, 112311, 22212322 };
      Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}", "Value", "Binary", "Octal", "Hex");
      foreach (int number in numbers) {
         Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}", 
                           number, Convert.ToString(number, 2), 
                           Convert.ToString(number, 8), 
                           Convert.ToString(number, 16));
      }      
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var