2012年7月30日 星期一

byte array和string轉換


'  VB.Net 把byte array轉成String
        Dim b As Byte() = {65, 66, 67, 68, 69, 70, 71}
        Dim str As String
        Dim enc As New System.Text.ASCIIEncoding()
        Response.Write(enc.GetString(b))

        '   VB.Net 把String 轉成byte array
        Dim encAs New System.Text.ASCIIEncoding()
        Dim b As Byte() = enc.GetBytes("ABCDEFG")
        For i As Integer = 0 To b.Length - 1
                Response.Write(b(i).ToString()+",")
        Next

        ' 將字串轉成byte陣列
        Shared Function StrToByteArray(ByVal str As String) As Byte()
            Dim encoding As New System.Text.ASCIIEncoding()
            Return encoding.GetBytes(str)
        End Function

        ' 將byte陣列轉成字串
        Shared Function ByteArrayToStr(ByVal bt As Byte()) As String
            Dim encoding As New System.Text.ASCIIEncoding()
            Return encoding.GetString(bt)
        End Function

沒有留言:

張貼留言