Encoding.Convert Method (Encoding, Encoding, Byte())
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts an entire byte array from one encoding to another.
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Shared Function Convert ( _ srcEncoding As Encoding, _ dstEncoding As Encoding, _ bytes As Byte() _ ) As Byte()
Parameters
- srcEncoding
- Type: System.Text.Encoding
The encoding format of bytes.
- dstEncoding
- Type: System.Text.Encoding
The target encoding format.
- bytes
- Type:
System.Byte
()
The bytes to convert.
Return Value
Type: System.Byte ()A byte array that contains the results of converting bytes from srcEncoding to dstEncoding.
| Exception | Condition |
|---|---|
| ArgumentNullException | srcEncoding is Nothing. -or- dstEncoding is Nothing. -or- bytes is Nothing. |
| DecoderFallbackException | A fallback occurred. |
| EncoderFallbackException | A fallback occurred. |
The following code example converts a string from one encoding to another.
Imports System.Text Class Example Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H3A0) & ")" ' Create two different encodings. Dim utf8 As Encoding = Encoding.UTF8 Dim unicode As Encoding = Encoding.Unicode ' Convert the string into a byte[]. Dim unicodeBytes As Byte() = unicode.GetBytes(unicodeString) ' Perform the conversion from one encoding to the other. Dim utf8Bytes As Byte() = Encoding.Convert(unicode, utf8, unicodeBytes) ' Convert the new byte[] into a char[] and then into a string. Dim utf8Chars(utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length) - 1) As Char utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0) Dim utf8String As New String(utf8Chars) ' Display the strings created before and after the conversion. outputBlock.Text += String.Format("Original string: {0}", unicodeString) & vbCrLf outputBlock.Text += String.Format("Ascii converted string: {0}", utf8String) & vbCrLf End Sub End Class ' The example displays the following output: ' Original string: This string contains the unicode character Pi (Π) ' Ascii converted string: This string contains the unicode character Pi (?)
Show: