加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VB.net中Byte数组转String、Long、Single...

(2010-05-10 16:35:05)
标签:

it

分类: .net

转为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

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有