SIGN UP MEMBER LOGIN:    
ARTICLE

Convert Bool to Byte Array in C#

Posted by Mahesh Chand Articles | How do I February 24, 2010
This article demonstrates how to convert boolean value to a byte array in C#.
Reader Level:

Convert Bool 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();       

    }

}

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor