System.BitConverter

This class has a number of methods that convert from raw (unmanaged) data to managed data types. Listing B.1 shows an example of converting from a byte array to an integer (bitconverter.cs).

Listing B.1. System.BitConverter Sample
static void Main(string [] args)
{
    byte [] rawData = new byte [] {1,2,3,4} ;
    int convertedInt = BitConverter.ToInt32(rawData, 0);
    Console.WriteLine("Raw data: {0} {1} {2} {3} ",
                      rawData[0],
                      rawData[1],
                      rawData[2],
                      rawData[3]);
    Console.WriteLine("Converted data: {0}  0x{0:X} ",
                      convertedInt);
}

The output for this application looks like this:

Raw data: 1234
Converted data: 67305985 0x4030201

Get .NET Common Language Runtime Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.