O'Reilly logo

.NET Common Language Runtime Unleashed by Kevin Burton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required