VB.Net by API/System.Text/Encoding

Материал из VB Эксперт
Версия от 12:50, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Encoding.ASCII

  
Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Imports System.Runtime.Serialization.Formatters
public class MainClass
   Shared Public Sub Main(ByVal CmdArgs() As String)
        If (CmdArgs.Length <> 1) Then
            Console.WriteLine("usage: TestKeyHash <filename>")
            End
        End If
        Dim key() As Byte = Encoding.ASCII.GetBytes("My Secret Key".ToCharArray())
        Dim hmac As HMACSHA1 = New HMACSHA1(key)
        Dim fs As FileStream = File.OpenRead(CmdArgs(0))
        Dim hash() As Byte = hmac.ruputeHash(fs)
        Dim b64 As String = Convert.ToBase64String(hash)
        fs.Close()
        Console.WriteLine(b64)
    End Sub
End Class


Encoding.Default

  
 
Imports System.Text
Imports System.IO

Public Class Tester
    Public Shared Sub Main
        Dim myFileStream As FileStream
        Dim myStreamReader As StreamReader
        Dim strRead As String = ""
        Dim StreamEncoding As Encoding
        Try
            StreamEncoding = Encoding.Default
            "StreamEncoding = Encoding.Unicode
            "StreamEncoding = Encoding.UTF8
            "StreamEncoding = Encoding.UTF7
            myFileStream = New FileStream("test.txt", FileMode.Open, FileAccess.Read)
            myStreamReader = New StreamReader(myFileStream, StreamEncoding)
            Console.WriteLine(myStreamReader.ReadToEnd)
            myFileStream.Close()
            myStreamReader.Close()
        Catch EX As IOException
            Console.WriteLine(EX.Message)
        End Try
    End Sub
End Class


Encoding.UTF32.GetString

  
Public Class Tester
    Public Shared Sub Main
        Dim quote As String = "qqqqqq"
        Dim result As New System.Text.StringBuilder
        Dim bytesUTF7 As Byte() = System.Text.Encoding.UTF7.GetBytes(quote)
        Dim bytesUTF8 As Byte() = System.Text.Encoding.UTF8.GetBytes(quote)
        Dim bytesUnicode As Byte() = System.Text.Encoding.Unicode.GetBytes(quote)
        Dim bytesUTF32 As Byte() = System.Text.Encoding.UTF32.GetBytes(quote)
        result.Append("bytesUTF7.Length = ")
        result.AppendLine(bytesUTF7.Length.ToString())
        result.Append("bytesUTF8.Length = ")
        result.AppendLine(bytesUTF8.Length.ToString())
        result.Append("bytesUnicode.Length = ")
        result.AppendLine(bytesUnicode.Length.ToString())
        result.Append("bytesUTF32.Length = ")
        result.AppendLine(bytesUTF32.Length.ToString())
        Dim fromUTF7 As String = System.Text.Encoding.UTF7.GetString(bytesUTF7)
        Dim fromUTF8 As String = System.Text.Encoding.UTF8.GetString(bytesUTF8)
        Dim fromUnicode As String = System.Text.Encoding.Unicode.GetString(bytesUnicode)
        Dim fromUTF32 As String = System.Text.Encoding.UTF32.GetString(bytesUTF32)
        If (fromUTF7 <> quote) Then _
           Throw New Exception("UTF7 Conversion Error")
        If (fromUTF8 <> quote) Then _
           Throw New Exception("UTF8 Conversion Error")
        If (fromUnicode <> quote) Then _
           Throw New Exception("Unicode Conversion Error")
        If (fromUTF32 <> quote) Then _
           Throw New Exception("UTF32 Conversion Error")
        Console.WriteLine(result.ToString())    
    End Sub
End Class


Encoding.UTF7.GetBytes

  
Public Class Tester
    Public Shared Sub Main
        Dim quote As String = "qqqqqq"
        Dim result As New System.Text.StringBuilder
        Dim bytesUTF7 As Byte() = System.Text.Encoding.UTF7.GetBytes(quote)
        Dim bytesUTF8 As Byte() = System.Text.Encoding.UTF8.GetBytes(quote)
        Dim bytesUnicode As Byte() = System.Text.Encoding.Unicode.GetBytes(quote)
        Dim bytesUTF32 As Byte() = System.Text.Encoding.UTF32.GetBytes(quote)
        result.Append("bytesUTF7.Length = ")
        result.AppendLine(bytesUTF7.Length.ToString())
        result.Append("bytesUTF8.Length = ")
        result.AppendLine(bytesUTF8.Length.ToString())
        result.Append("bytesUnicode.Length = ")
        result.AppendLine(bytesUnicode.Length.ToString())
        result.Append("bytesUTF32.Length = ")
        result.AppendLine(bytesUTF32.Length.ToString())
        Dim fromUTF7 As String = System.Text.Encoding.UTF7.GetString(bytesUTF7)
        Dim fromUTF8 As String = System.Text.Encoding.UTF8.GetString(bytesUTF8)
        Dim fromUnicode As String = System.Text.Encoding.Unicode.GetString(bytesUnicode)
        Dim fromUTF32 As String = System.Text.Encoding.UTF32.GetString(bytesUTF32)
        If (fromUTF7 <> quote) Then _
           Throw New Exception("UTF7 Conversion Error")
        If (fromUTF8 <> quote) Then _
           Throw New Exception("UTF8 Conversion Error")
        If (fromUnicode <> quote) Then _
           Throw New Exception("Unicode Conversion Error")
        If (fromUTF32 <> quote) Then _
           Throw New Exception("UTF32 Conversion Error")
        Console.WriteLine(result.ToString())    
    End Sub
End Class