Export (0) Print
Expand All

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[]

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft