Convert Value Type to Byte Array and Vice Versa in C#

No.of Views1657
Bookmarked0 times
Downloads 
Votes0
By  kirtan007   On  19 Jun 2010 21:06:58
Tag : CSharp , Miscellaneous
In this codesnippet you will learn how to convert decimal to byte array and byte array to decimal again.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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.

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 Arraybyte[] ByteArray = BitConverter.GetBytes(TestInt);//Retrive Int Again from Byte Arrayint 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

/* 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 ByteArrayMemoryStream 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.

Sample Project Source

Download source files -24 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top