分享

Byte Array to String

 履历馆 2011-12-08

转为String的方法,System.Text.UTF8Encoding.Unicode.GetString(buffer)

 

 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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约