SIGN UP MEMBER LOGIN:    
ARTICLE

Convert Value Type to Byte Array and Vice Versa

Posted by Kirtan Patel Articles | Current Affairs February 22, 2010
In this article you will learn how to convert decimal to byte array and byte array to decimal again.
Reader Level:
Download Files:
 

Introduction

Basic data type such as bool, int, char, uint ,float etc .can be converted to byte array simply using the System.BitConverter Class provided by .net but it does not convert decimal values to byte array. In this article we will learn how to convert decimal to byte array.

Language:

C Sharp
 
Technologies:

.net 2.0/3.5
 
Implementation

First we will look at how to convert int , char,bool etc to byte array .

As I told you in introduction of this article they can be converted using System.BitConverter Class.

Here is it is how it can be done.

  /* Converting Int <-> Byte Array */

            int TestInt = 50;

            //Convert it to Byte Array

            byte[] ByteArray = BitConverter.GetBytes(TestInt);

            //Retrive Int Again from Byte Array

            int OrigionalInt =BitConverter.ToInt32(ByteArray,0);

            Console.WriteLine("Integer Retrived from Byte Array :" + OrigionalInt);

Same way you can convert others like bool,char ,int.

Now let's take a look how to work with decimal.

Decimal's little bit different from normal int to byte array we need to work with memory Stream Object and BinaryWritter.

Here is code how we can do it.

Converting Decimal to Byte Array and Vice Versa :

/* Convert Decimal <-> Byte Array */

            decimal dec = 50.50M;

            byte[] bArray= null;

            MemoryStream memStream = new MemoryStream();

            BinaryWriter writer = new BinaryWriter(memStream);

            writer.Write(dec);

            bArray = memStream.ToArray();

            memStream.Close();

            writer.Close();

            //Get Decimal Value Back From ByteArray

            MemoryStream m2 = new MemoryStream(bArray);

            BinaryReader reader = new BinaryReader(m2);

            decimal d = reader.ReadDecimal();

            m2.Close();

            reader.Close();

            Console.WriteLine("Retrived Decimal From Binary Array :" + d.ToString());

            Console.ReadKey();

Conclusion

This article shows how to convert decimal to byte array and byte array to decimal again.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
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. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor