Convert Long, Short, Single to Byte Array in C#

The BitConverter class in .NET Framework is provides functionality to convert base data types to an array of bytes, and an array of bytes to base data types.

The BitConverter class has a static overloaded GetBytes method that takes an integer, double, bool, short, long, or other base type value and convert that to a array of bytes. The BitConverter class also has other static methods to reverse this conversion. Some of these methods are ToDouble, ToChart, ToBoolean, ToInt16, and ToSingle.

The following code snippet converts different float, long, short, char, and boolean values to a byte array and vice-versa.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BitConverterSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Bool, Char, Long, Short, Single and byte arrays conversion sample.");
            float floatValue = 12.098F;
            Console.WriteLine("Float value: " + floatValue.ToString());
            byte[] bytes = BitConverter.GetBytes(floatValue);
            Console.WriteLine("Byte array value:");
            Console.WriteLine(BitConverter.ToString(bytes));
            // Create byte array to Float
            float valueBack = BitConverter.ToSingle(bytes, 0);
            Console.WriteLine(valueBack.ToString());
            Console.WriteLine("--------------------------");
            long longValue = 1234567891011121314;
            Console.WriteLine("Long value: " + longValue.ToString());
            bytes = BitConverter.GetBytes(longValue);
            Console.WriteLine("Byte array value:");
            Console.WriteLine(BitConverter.ToString(bytes));
            // Create byte array to Long
            long longValueBack = BitConverter.ToInt64(bytes, 0);
            Console.WriteLine(longValueBack.ToString());
            Console.WriteLine("--------------------------");
            short shortValue = 12345;
            Console.WriteLine("Short value: " + shortValue.ToString());
            bytes = BitConverter.GetBytes(shortValue);
            Console.WriteLine("Byte array value:");
            Console.WriteLine(BitConverter.ToString(bytes));
            // Create byte array to Short
            short shortValueBack = BitConverter.ToInt16(bytes, 0);
            Console.WriteLine(shortValueBack.ToString());
            Console.WriteLine("--------------------------");
            char charValue = 'c';
            Console.WriteLine("Char value: " + charValue.ToString());
            bytes = BitConverter.GetBytes(charValue);
            Console.WriteLine("Byte array value:");
            Console.WriteLine(BitConverter.ToString(bytes));
            // Create byte array to Char
            char charValueBack = BitConverter.ToChar(bytes, 0);
            Console.WriteLine(charValueBack.ToString());
            Console.WriteLine("--------------------------");
            bool boolValue = true;
            Console.WriteLine("Bool value: " + boolValue.ToString());
            bytes = BitConverter.GetBytes(boolValue);
            Console.WriteLine("Byte array value:");
            Console.WriteLine(BitConverter.ToString(bytes));
            // Create byte array to Bool
            bool boolValueBack = BitConverter.ToBoolean(bytes, 0);
            Console.WriteLine(boolValueBack.ToString());
            Console.WriteLine("--------------------------");
            Console.ReadLine();
    }
}

 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.