UTF8Encoding.GetString(Byte[], Int32, Int32) 메서드

정의

바이트 배열의 바이트 범위를 문자열로 디코딩합니다.

public:
 override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int index, int count);
public override string GetString (byte[] bytes, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override string GetString (byte[] bytes, int index, int count);
override this.GetString : byte[] * int * int -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetString : byte[] * int * int -> string
Public Overrides Function GetString (bytes As Byte(), index As Integer, count As Integer) As String

매개 변수

bytes
Byte[]

디코딩할 바이트 시퀀스를 포함하는 바이트 배열입니다.

index
Int32

디코딩할 첫 번째 바이트의 인덱스입니다.

count
Int32

디코딩할 바이트 수입니다.

반환

String

지정된 바이트 시퀀스에 대한 디코딩 결과가 포함된 String입니다.

특성

예외

bytes이(가) null인 경우

index 또는 count가 0보다 작습니다.

또는 indexcountbytes에서 올바른 범위를 나타내지 않습니다.

오류 검색이 사용되고 bytes에 잘못된 바이트 시퀀스를 포함합니다.

대체가 발생했습니다(자세한 내용은 .NET의 문자 인코딩 참조). 및 DecoderFallbackDecoderExceptionFallback로 설정됩니다.

예제

다음 예제에서는 메서드를 호출 GetByteCount 하여 인코딩된 문자열에 필요한 바이트 수를 정확히 확인한 다음 BOM(바이트 순서 표시)의 크기를 추가하여 배열을 초기화합니다. 그런 다음, 메서드를 GetPreamble 호출하여 인코딩된 바이트를 배열에 저장하기 전에 GetBytes BOM을 배열에 저장합니다. 그런 다음, 메서드를 GetString 호출하여 문자열을 디코딩합니다.

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      UTF8Encoding utf8 = new UTF8Encoding(true, true);

      String s = "It was the best of times, it was the worst of times...";

      // We need to dimension the array, since we'll populate it with 2 method calls.
      Byte[] bytes = new Byte[utf8.GetByteCount(s) + utf8.GetPreamble().Length];
      // Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length);
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length);

      // Decode the byte array.
      String s2 = utf8.GetString(bytes, 0, bytes.Length);
      Console.WriteLine(s2);
   }
}
// The example displays the following output:
//        ?It was the best of times, it was the worst of times...
Imports System.Text

Module Example
   Public Sub Main()
      Dim utf8 As New UTF8Encoding(True, True)

      Dim s As String = "It was the best of times, it was the worst of times..."

      ' We need to dimension the array, since we'll populate it with 2 method calls.
      Dim bytes(utf8.GetByteCount(s) + utf8.GetPreamble().Length - 1) As Byte
      ' Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length)
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length)

      ' Decode the byte array.
      Dim s2 As String = utf8.GetString(bytes, 0, bytes.Length)
      Console.WriteLine(s2)
   End Sub
End Module
' The example displays the following output:
'       ?It was the best of times, it was the worst of times...

이 경우 디코딩된 문자열은 16비트 바이트 순서 표시 U+FFFD로 시작하므로 원래 문자열과 다릅니다. 즉, 두 문자열이 같지 않은 것으로 비교되고 문자열이 출력되면 BOM이 대체 문자 "?"로 표시됩니다. 문자열의 시작 부분에서 BOM을 제거하려면 메서드를 호출할 String.TrimStart 수 있습니다.

설명

오류 검색 시퀀스가 잘못되면 이 메서드가 예외를 throw합니다 ArgumentException . 오류 검색이 없으면 잘못된 시퀀스가 무시되고 예외가 throw되지 않습니다.

디코딩할 바이트 범위에 BOM(바이트 순서 표시)이 포함되고 바이트 배열이 BOM 인식이 아닌 형식의 메서드에 의해 반환된 경우 U+FFFE 문자가 이 메서드에서 반환된 문자 배열에 포함됩니다. 메서드를 호출 String.TrimStart 하여 제거할 수 있습니다.

스트림에서 읽은 데이터와 같이 변환할 데이터는 순차적 블록에서만 사용할 수 있습니다. 이 경우 또는 데이터의 양이 너무 커서 더 작은 블록으로 나눠야 하는 경우 메서드 또는 메서드에서 제공하는 GetDecoder 것을 각각 사용합니다Decoder.GetEncoder Encoder

적용 대상

추가 정보