Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Convert byte array to char – double – int and single using BitConverter class - VB.net - C# .net.

Author: Shehzad Hemani
Download Source Code : 1010_source.zip

In this simple article you will learn that how we can convert byte array to character value, double value, integer value and single value using BitConverter class VB.net - C# .net.

 Byte Array TO Char:

To convert byte array to char value, we have static method of BitConverter class named ToChar (). This method takes byte array and starting index of the byte array from where conversion should start and returns a char value.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

private void btn_convertchar_Click(object sender, EventArgs e)

        {

            char c = 'a';

            byte[] br = BitConverter.GetBytes(c);

            string str = BitConverter.ToString(br);

            MessageBox.Show("BYTE ARRAY IS " + str + "\nCHARACTER IS " + BitConverter.ToChar(br, 0));

        }

VB

Private Sub btn_convertchar_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim c As Char = "a"c

        Dim br As Byte() = BitConverter.GetBytes(c)

        Dim str As String = BitConverter.ToString(br)

        MessageBox.Show(("BYTE ARRAY IS " & str & vbLf & "CHARACTER IS ") + BitConverter.ToChar(br, 0))

    End Sub

This is simple code to convert byte array to char value.

Byte Array to Double:

To convert byte array to double value, we have static method of BitConverter class named ToDouble (). This method takes byte array and starting index of the byte array from where conversion should start and returns a double value.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

private void btn_double_Click(object sender, EventArgs e)

        {

            double d = 1.11111111111111;

            byte[] br = BitConverter.GetBytes(d);

            string str = BitConverter.ToString(br);

            MessageBox.Show("BYTE ARRAY IS " + str + "\nDouble value IS " + BitConverter.ToDouble(br, 0));

        }

VB

Private Sub btn_double_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim d As Double = 1.11111111111111

        Dim br As Byte() = BitConverter.GetBytes(d)

        Dim str As String = BitConverter.ToString(br)

        MessageBox.Show(("BYTE ARRAY IS " & str & vbLf & "Double value IS ") + BitConverter.ToDouble(br, 0))

    End Sub

This is simple code to convert byte array to double value.

Byte Array to Single:

To convert byte array to single value, we have static method of BitConverter class named ToSingle (). This method takes byte array and starting index of the byte array from where conversion should start and returns a single value.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

private void btn_single_Click(object sender, EventArgs e)

        {

            Single s = 2.5f;

            byte[] br = BitConverter.GetBytes(s);

            string str = BitConverter.ToString(br);

            MessageBox.Show("BYTE ARRAY IS " + str + "\nSingle value IS " + BitConverter.ToSingle(br, 0));

        }

VB

Private Sub btn_single_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim s As [Single] = 2.5F

        Dim br As Byte() = BitConverter.GetBytes(s)

        Dim str As String = BitConverter.ToString(br)

        MessageBox.Show(("BYTE ARRAY IS " & str & vbLf & "Single value IS ") + BitConverter.ToSingle(br, 0))

    End Sub

This is simple code to convert byte array to single value.

Byte Array to Integer:

To convert byte array to Integer value, we have static method of BitConverter class named ToInt32 (). This method takes byte array and starting index of the byte array from where conversion should start and returns an integer value.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

private void btn_int_Click(object sender, EventArgs e)

        {

            int d = 100;

            byte[] br = BitConverter.GetBytes(d);

            string str = BitConverter.ToString(br);

            MessageBox.Show("BYTE ARRAY IS " + str + "\nInteger value IS " + BitConverter.ToInt32(br, 0));

        }

VB

Private Sub btn_int_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim d As Integer = 100

        Dim br As Byte() = BitConverter.GetBytes(d)

        Dim str As String = BitConverter.ToString(br)

        MessageBox.Show(("BYTE ARRAY IS " & str & vbLf & "Integer value IS ") + BitConverter.ToInt32(br, 0))

    End Sub

This is simple code to convert byte array to integer value.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP BIT CONVERTER APPLICATION";

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP BIT CONVERTER APPLICATION"

    End Sub

This simple article tells that how we can convert byte array to character value, double value, integer value and single value using BitConverter class in VB and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Compare two strings - get culture – ordinal - invariant culture (ignoring case) of string comparer using string comparer class vb.net and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net