Wednesday, March 26, 2008

How to convert simple types (basic operations) in c#.

To convert one simple type (for example int), to another simple type (for example float or string) it is suggested to use .NET class BitConverter.

C# code sample:

static void Main()
{
int intData = 0x01020304;
byte[] bytesData = BitConverter. GetBytes(intData);
float floatData = BitConverter.ToSingle(bytesData, 0);
string stringData = BitConverter.ToString(bytesData);
}


Result values (from VS Locals Window):


BitConverterResultsInLocals



Reference:



BitConverter Class

No comments: