This documentation is archived and is not being maintained.

BitConverter.GetBytes Method (UInt32)

Returns the specified 32-bit unsigned integer value as an array of bytes.

This method is not CLS-compliant.  

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

[CLSCompliantAttribute(false)] 
public static byte[] GetBytes (
	uint value
)
/** @attribute CLSCompliantAttribute(false) */ 
public static byte[] GetBytes (
	UInt32 value
)
CLSCompliantAttribute(false) 
public static function GetBytes (
	value : uint
) : byte[]
Not applicable.

Parameters

value

The number to convert.

Return Value

An array of bytes with length 4.

The following code example converts the bit patterns of UInt32 values to Byte arrays with the GetBytes method.

// Example of the BitConverter.GetBytes( uint ) method.
using System;

class GetBytesUInt32Demo
{
    const string formatter = "{0,16}{1,20}";
 
    // Convert a uint argument to a byte array and display it.
    public static void GetBytesUInt32( uint argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument, 
            BitConverter.ToString( byteArray ) );
    }
       
    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the BitConverter.GetBytes( uint ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "uint", "byte array" );
        Console.WriteLine( formatter, "----", "----------" );
          
        // Convert uint values and display the results.
        GetBytesUInt32( 15 );
        GetBytesUInt32( 1023 );
        GetBytesUInt32( 0x100000 );
        GetBytesUInt32( 1000000000 );
        GetBytesUInt32( uint.MinValue );
        GetBytesUInt32( int.MaxValue );
        GetBytesUInt32( uint.MaxValue );
    }
}

/*
This example of the BitConverter.GetBytes( uint )
method generates the following output.

            uint          byte array
            ----          ----------
              15         0F-00-00-00
            1023         FF-03-00-00
         1048576         00-00-10-00
      1000000000         00-CA-9A-3B
               0         00-00-00-00
      2147483647         FF-FF-FF-7F
      4294967295         FF-FF-FF-FF
*/

// Example of the BitConverter.GetBytes( uint ) method.
import System.*;

class GetBytesUInt32Demo
{
    private static String formatter = "{0,16}{1,20}";

    // Convert a uint argument to a byte array and display it.
    public static void GetBytesUInt32(UInt32 argument)
    {
        ubyte byteArray[] = BitConverter.GetBytes(argument);
        Console.WriteLine(formatter, (UInt32)argument, 
            BitConverter.ToString(byteArray));
    } //GetBytesUInt32

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of the BitConverter.GetBytes( uint ) " 
            + "\nmethod generates the following output.\n"));
        Console.WriteLine(formatter, "uint", "byte array");
        Console.WriteLine(formatter, "----", "----------");

        // Convert uint values and display the results.
        GetBytesUInt32((UInt32)15);
        GetBytesUInt32((UInt32)1023);
        GetBytesUInt32((UInt32)0x100000);
        GetBytesUInt32((UInt32)1000000000);
        GetBytesUInt32(UInt32.MinValue);
        GetBytesUInt32((UInt32)Int32.MaxValue);
        GetBytesUInt32(UInt32.MaxValue);
    } //main
} //GetBytesUInt32Demo

/*
This example of the BitConverter.GetBytes( uint )
method generates the following output.

            uint          byte array
            ----          ----------
              15         0F-00-00-00
            1023         FF-03-00-00
         1048576         00-00-10-00
      1000000000         00-CA-9A-3B
               0         00-00-00-00
      2147483647         FF-FF-FF-7F
      4294967295         FF-FF-FF-FF
*/

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
Show: