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 char – double – int and single to byte array using BitConverter class - VB.net - C# .net.

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

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

 Bit Converter Class:

Bit converter class is used to converts base data types to an array of bytes, and an array of bytes to base data types.

Char to Byte Array:

To convert char value to byte array, we have static method of BitConverter class named GetBytes (). This method takes char value as parameter and returns byte array.

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

Now write the following code on Button click event:

C#

private void btn_convertchar_Click(object sender, EventArgs e)

        {

            if (txt_char.Text != "")

            {

                char c = Convert.ToChar(txt_char.Text);

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

                string str = BitConverter.ToString(br);

                MessageBox.Show(str);

            }

            else

                MessageBox.Show("enter value");

        }

VB

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

        If txt_char.Text <> "" Then

            Dim c As Char = Convert.ToChar(txt_char.Text)

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

            Dim str As String = BitConverter.ToString(br)

            MessageBox.Show(str)

        Else

            MessageBox.Show("enter value")

        End If

    End Sub

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

Double to Byte Array:

To convert double value to byte array, we have static method of BitConverter class named GetBytes (). This method takes double value as parameter and returns byte array.

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

Now write the following code on Button click event:

C#

private void btn_double_Click(object sender, EventArgs e)

        {

            if (txt_double.Text != "")

            {

                double d = Convert.ToDouble(txt_double.Text);

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

                string str = BitConverter.ToString(br);

                MessageBox.Show(str);

            }

            else

                MessageBox.Show("enter value");

        }

VB

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

        If txt_double.Text <> "" Then

            Dim d As Double = Convert.ToDouble(txt_double.Text)

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

            Dim str As String = BitConverter.ToString(br)

            MessageBox.Show(str)

        Else

            MessageBox.Show("enter value")

        End If

    End Sub

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

Single to Byte Array:

To convert single value to byte array, we have static method of BitConverter class named GetBytes (). This method takes single value as parameter and returns byte array.

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

Now write the following code on Button click event:

C#

private void btn_single_Click(object sender, EventArgs e)

        {

            if (txt_single.Text != "")

            {

                Single s = Convert.ToSingle(txt_single.Text);

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

                string str = BitConverter.ToString(br);

                MessageBox.Show(str);

            }

            else

                MessageBox.Show("enter value");

        }

VB

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

        If txt_single.Text <> "" Then

            Dim s As [Single] = Convert.ToSingle(txt_single.Text)

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

            Dim str As String = BitConverter.ToString(br)

            MessageBox.Show(str)

        Else

            MessageBox.Show("enter value")

        End If

    End Sub

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

Integer to Byte Array:

To convert integer value to byte array, we have static method of BitConverter class named GetBytes (). This method takes integer value as parameter and returns byte array.

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

Now write the following code on Button click event:

C#

private void btn_int_Click(object sender, EventArgs e)

        {

            if (txt_int.Text != "")

            {

                int i = Convert.ToInt32(txt_int.Text);

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

                string str = BitConverter.ToString(br);

                MessageBox.Show(str);

            }

            else

                MessageBox.Show("enter value");

        }

VB

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

        If txt_int.Text <> "" Then

            Dim i As Integer = Convert.ToInt32(txt_int.Text)

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

            Dim str As String = BitConverter.ToString(br)

            MessageBox.Show(str)

        Else

            MessageBox.Show("enter value")

        End If

    End Sub

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

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 character value, double value, int value and single value to byte array using BitConverter class in VB and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Use, Initialize, get - set the key and value of the object of dictionary entry collection using DictionaryEntry class vb.net - C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net