This topic has not yet been rated - Rate this topic

BitConverter.ToUInt32 Method

Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.

This API is not CLS-compliant. 

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[CLSCompliantAttribute(false)]
public static uint ToUInt32(
	byte[] value,
	int startIndex
)

Parameters

value
Type: System.Byte[]
An array of bytes.
startIndex
Type: System.Int32
The starting position within value.

Return Value

Type: System.UInt32
A 32-bit unsigned integer formed by four bytes beginning at startIndex.
Exception Condition
ArgumentException

startIndex is greater than or equal to the length of value minus 3, and is less than or equal to the length of value minus 1.

ArgumentNullException

value is null.

ArgumentOutOfRangeException

startIndex is less than zero or greater than the length of value minus 1.

The following code example converts elements of Byte arrays to UInt32 values with the ToUInt32 method.


// Example of the BitConverter.ToUInt32 method.
using System;

class BytesToUInt32Demo
{
    const string formatter = "{0,5}{1,17}{2,15}";

    // Convert four byte array elements to a uint and display it.
    public static void BAToUInt32( byte[ ] bytes, int index )
    {
        uint value = BitConverter.ToUInt32( bytes, index );

        Console.WriteLine( formatter, index, 
            BitConverter.ToString( bytes, index, 4 ), value );
    }

    public static void Main( )
    {
        byte[ ] byteArray = {
             15,   0,   0,   0,   0,  16,   0, 255,   3,   0, 
              0, 202, 154,  59, 255, 255, 255, 255, 127 };

        Console.WriteLine( 
            "This example of the BitConverter.ToUInt32( byte[ ], " +
            "int ) \nmethod generates the following output. It " +
            "converts elements \nof a byte array to uint values.\n" );
        Console.WriteLine( "initial byte array" );
        Console.WriteLine( "------------------" );
        Console.WriteLine( BitConverter.ToString( byteArray ) );
        Console.WriteLine( );
        Console.WriteLine( formatter, "index", "array elements", 
            "uint" );
        Console.WriteLine( formatter, "-----", "--------------", 
            "----" );

        // Convert byte array elements to uint values.
        BAToUInt32( byteArray, 1 );
        BAToUInt32( byteArray, 0 );
        BAToUInt32( byteArray, 7 );
        BAToUInt32( byteArray, 3 );
        BAToUInt32( byteArray, 10 );
        BAToUInt32( byteArray, 15 );
        BAToUInt32( byteArray, 14 );
    }
}

/*
This example of the BitConverter.ToUInt32( byte[ ], int )
method generates the following output. It converts elements
of a byte array to uint values.

initial byte array
------------------
0F-00-00-00-00-10-00-FF-03-00-00-CA-9A-3B-FF-FF-FF-FF-7F

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Is there a bug?

Ok, so I think there is a bug with this method in .NET 3.5 SP1 using Visual Studio 2008. Multiple projects dupe this. I haven't tested .NET 4 yet.

I have this code:

Byte[] b = { 1, 0, 10, 1 };
uint u = BitConverter.ToUInt32(b, 0);


u then equals 17432577 which is these bits:
00000001 00001010 00000000 00000001

That is totally, wrong. It is like it switched the second and third bytes.

u should equal 16779777 which is these bits:
000000001 00000000 00001010 00000001

This is the correct behavior


In performing the conversion, the relationship between the order of individual bytes in the array and the individual bits in the returned UInt32 value depends on the endianness of the computer architecture. You can determine the endianness of a system from the BitConverter.IsLittleEndian property. Since you're running on a little endian system, the byte at index 0 in the array becomes the low-order byte of the UInt32 value, and the byte at index 3 becomes the high-order byte of the UInt32 value. That is why the second and third bytes appear to be switched.

You can see this more clearly by defining a byte array that does not have two bytes with a value of 1 and converting it to an unsigned integer:

       byte[] b = { 1, 0, 0, 0 };
Console.WriteLine(BitConverter.ToUInt32(b, 0)); // Displays 1


The documentation for the BitConverter Class at http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx discusses this issue and notes that the ToXXX methods reflect an architecture's endianness:

The order of bytes in the array returned by the GetBytes method overloads (as well as the order of bits in the integer returned by the DoubleToInt64Bits method and the order of hexadecimal strings returned by the ToString(array<Byte>[]()[]) method) depends on whether the computer architecture is little-endian or big-endian. The endianness of an architecture is indicated by the IsLittleEndian property, which returns true on little-endian systems and false on big-endian systems. On little-endian systems, lower-order bytes precede higher-order bytes. On big-endian system, higher-order bytes precede lower-order bytes.

--Ron Petrusha
CLR Developer Content Team
Microsoft Corporation