Export (0) Print
Expand All

BitConverter Class

Converts base data types to an array of bytes, and an array of bytes to base data types.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static class BitConverter
public final class BitConverter
public final class BitConverter
Not applicable.

This class facilitates manipulating value types in their fundamental form. A byte is defined as an 8-bit unsigned integer.

The following code example illustrates the use of several BitConverter class methods.

// Example of BitConverter class methods.
using System;

class BitConverterDemo
{
    public static void Main( )
    {
        const string formatter = "{0,25}{1,30}";
 
        double  aDoubl  = 0.1111111111111111111;
        float   aSingl  = 0.1111111111111111111F;
        long    aLong   = 1111111111111111111;
        int     anInt   = 1111111111;
        short   aShort  = 11111;
        char    aChar   = '*';
        bool    aBool   = true;

        Console.WriteLine( 
            "This example of methods of the BitConverter class" +
            "\ngenerates the following output.\n" );
        Console.WriteLine( formatter, "argument", "byte array" );
        Console.WriteLine( formatter, "--------", "----------" );

        // Convert values to Byte arrays and display them.
        Console.WriteLine( formatter, aDoubl, 
            BitConverter.ToString( BitConverter.GetBytes( aDoubl ) ) );
        Console.WriteLine( formatter, aSingl, 
            BitConverter.ToString( BitConverter.GetBytes( aSingl ) ) );
        Console.WriteLine( formatter, aLong, 
            BitConverter.ToString( BitConverter.GetBytes( aLong ) ) );
        Console.WriteLine( formatter, anInt, 
            BitConverter.ToString( BitConverter.GetBytes( anInt ) ) );
        Console.WriteLine( formatter, aShort, 
            BitConverter.ToString( BitConverter.GetBytes( aShort ) ) );
        Console.WriteLine( formatter, aChar, 
            BitConverter.ToString( BitConverter.GetBytes( aChar ) ) );
        Console.WriteLine( formatter, aBool, 
            BitConverter.ToString( BitConverter.GetBytes( aBool ) ) );
    }
}

/*
This example of methods of the BitConverter class
generates the following output.

                 argument                    byte array
                 --------                    ----------
        0.111111111111111       1C-C7-71-1C-C7-71-BC-3F
                0.1111111                   39-8E-E3-3D
      1111111111111111111       C7-71-C4-2B-AB-75-6B-0F
               1111111111                   C7-35-3A-42
                    11111                         67-2B
                        *                         2A-00
                     True                            01
*/

// Example of BitConverter class methods.
import System.*;

class BitConverterDemo 
{
    public static void main(String[] args) 
    {
        final System.String formatter = "{0,25}{1,30}";

        double aDoubl = 0.1111111111111111111;
        float aSingl = 0.1111111111111111111F;
        long aLong = 1111111111111111111L;
        int anInt = 1111111111;
        short aShort = 11111;
        char aChar = '*';
        boolean aBool = true;

        Console.WriteLine(("This example of methods of the BitConverter class"
            + "\ngenerates the following output.\n"));
        Console.WriteLine(formatter, "argument", "byte array");
        Console.WriteLine(formatter, "--------", "----------");

        // Convert values to Byte arrays and display them.
        Console.WriteLine(formatter, (System.Double)aDoubl, 
            BitConverter.ToString(BitConverter.GetBytes(aDoubl)));
        Console.WriteLine(formatter, (System.Single)aSingl, 
            BitConverter.ToString(BitConverter.GetBytes(aSingl)));
        Console.WriteLine(formatter, (Int64)aLong, 
            BitConverter.ToString(BitConverter.GetBytes(aLong)));
        Console.WriteLine(formatter, (Int32)anInt, 
            BitConverter.ToString(BitConverter.GetBytes(anInt)));
        Console.WriteLine(formatter, (Int16)aShort, 
            BitConverter.ToString(BitConverter.GetBytes(aShort)));
        Console.WriteLine(formatter, (System.Char)aChar, 
            BitConverter.ToString(BitConverter.GetBytes(aChar)));
        Console.WriteLine(formatter, (System.Boolean)aBool, 
            BitConverter.ToString(BitConverter.GetBytes(aBool)));
    } //main
} //BitConverterDemo
/*
This example of methods of the BitConverter class
generates the following output.

                 argument                    byte array
                 --------                    ----------
        0.111111111111111       1C-C7-71-1C-C7-71-BC-3F
                0.1111111                   39-8E-E3-3D
      1111111111111111111       C7-71-C4-2B-AB-75-6B-0F
               1111111111                   C7-35-3A-42
                    11111                         67-2B
                        *                         2A-00
                     True                            01
*/

System.Object
  System.BitConverter

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft