SIGN UP MEMBER LOGIN:    
ARTICLE

Convert Integer to Byte Array in C#

Posted by Mahesh Chand Articles | C# Language February 11, 2010
The code snippet in this article converts different integer values to a byte array and vice-versa using BitConverter class.
Reader Level:

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 or other base type value and convert that to a array of bytes. The BitConverter class also have other static methods to reverse this conversion. Some of these methods are ToDouble, ToChart, ToBoolean, ToInt16, and ToSingle.

The following code snippet converts different integer values to a byte array and vice-versa.

namespace BitConverterSample

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Int and byte arrays conversion sample.");

            // Create double to a byte array

            Int32 i32 = 125;

            Console.WriteLine("Int value: " + i32.ToString());

            byte[] bytes = ConvertInt32ToByteArray(i32);

            Console.WriteLine("Byte array value:");

            Console.WriteLine(BitConverter.ToString(bytes));

 

            Console.WriteLine("Byte array back to Int32:");

            // Create byte array to Int32

            double dValue = ConvertByteArrayToInt32(bytes);

            Console.WriteLine(dValue.ToString());

 

            Console.ReadLine();

       }

        public static byte[] ConvertInt32ToByteArray(Int32 I32)

        {

            return BitConverter.GetBytes(I32);

        }

        public static byte[] ConvertIntToByteArray(Int16 I16)

        {

            return BitConverter.GetBytes(I16);

        }

        public static byte[] ConvertIntToByteArray(Int64 I64)

        {

            return BitConverter.GetBytes(I64);

        }

        public static byte[] ConvertIntToByteArray(int I)

        {

            return BitConverter.GetBytes(I);

        }

     

 

        public static double ConvertByteArrayToInt32(byte[] b)

        {

            return BitConverter.ToInt32(b, 0);

        }

    }

}

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor