How to

convert numbers to bytes and vice versa

Published: 2. November 2011 | Updated: 2. November 2011
License: Microsoft Public License (MS-PL)
Categories: Converting » Numbers
Tags: C# Conversion
Was this snippet helpful for you? YESYES / NONO
// short / Int16
byte[] bytes = BitConverter.GetBytes((short)350);
short shortValue = BitConverter.ToInt16(bytes, 0);

// int / Int32
bytes = BitConverter.GetBytes(9999999);
int intValue = BitConverter.ToInt32(bytes, 0);

// long / Int64
bytes = BitConverter.GetBytes(8785456456454147446);
long longValue = BitConverter.ToInt64(bytes, 0);

// double
bytes = BitConverter.GetBytes(123.64978d);
double doubleValue = BitConverter.ToDouble(bytes, 0);

// float / single
bytes = BitConverter.GetBytes(48.18f);
float floatValue = BitConverter.ToSingle(bytes, 0);
Send us feedback about this snippet »



Related Snippets: