Encoding.GetString 메서드

정의

파생 클래스에서 재정의되면 바이트 시퀀스를 문자열로 디코딩합니다.

오버로드

GetString(Byte[])

파생 클래스에서 재정의되면 지정한 바이트 배열의 모든 바이트를 문자열로 디코딩합니다.

GetString(ReadOnlySpan<Byte>)

파생 클래스에서 재정의할 경우, 지정된 바이트 범위의 모든 바이트를 문자열로 디코딩합니다.

GetString(Byte*, Int32)

파생 클래스에서 재정의할 때 지정된 주소에서 시작하는 지정된 바이트 수를 문자열로 디코딩합니다.

GetString(Byte[], Int32, Int32)

파생 클래스에서 재정의되면 지정한 바이트 배열의 바이트 시퀀스를 문자열로 디코딩합니다.

GetString(Byte[])

파생 클래스에서 재정의되면 지정한 바이트 배열의 모든 바이트를 문자열로 디코딩합니다.

public:
 virtual System::String ^ GetString(cli::array <System::Byte> ^ bytes);
public virtual string GetString (byte[] bytes);
abstract member GetString : byte[] -> string
override this.GetString : byte[] -> string
Public Overridable Function GetString (bytes As Byte()) As String

매개 변수

bytes
Byte[]

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

반환

String

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

예외

바이트 배열에 잘못된 유니코드 코드 포인트가 포함되어 있습니다.

bytes이(가) null인 경우

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

DecoderFallbackDecoderExceptionFallback로 설정됩니다.

예제

다음 예제에서는 개체가 나타내는 이진 파일에서 u t f-8로 인코딩된 문자열을 읽습니다 FileStream . 2048 바이트 보다 작은 파일의 경우 전체 파일의 내용을 바이트 배열로 읽고 메서드를 호출 GetString(Byte[]) 하 여 디코딩을 수행 합니다. 큰 파일의 경우 바이트 배열로 2048 바이트를 읽고, 메서드를 호출 Decoder.GetCharCount(Byte[], Int32, Int32) 하 여 배열에 포함 된 문자 수를 확인 한 다음 메서드를 호출 하 여 Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32) 디코딩을 수행 합니다.

using System;
using System.IO;
using System.Text;

public class Example
{
   const int MAX_BUFFER_SIZE = 2048;
   static Encoding enc8 = Encoding.UTF8;

   public static void Main()
   {
      FileStream fStream = new FileStream(@".\Utf8Example.txt", FileMode.Open);
      string contents = null;
      
      // If file size is small, read in a single operation.
      if (fStream.Length <= MAX_BUFFER_SIZE) {
         Byte[] bytes = new Byte[fStream.Length];
         fStream.Read(bytes, 0, bytes.Length);
         contents = enc8.GetString(bytes);
      }
      // If file size exceeds buffer size, perform multiple reads.
      else {
         contents = ReadFromBuffer(fStream);
      }
      fStream.Close();
      Console.WriteLine(contents);
   }

   private static string ReadFromBuffer(FileStream fStream)
   {
        Byte[] bytes = new Byte[MAX_BUFFER_SIZE];
        string output = String.Empty;
        Decoder decoder8 = enc8.GetDecoder();
      
        while (fStream.Position < fStream.Length) {
           int nBytes = fStream.Read(bytes, 0, bytes.Length);
           int nChars = decoder8.GetCharCount(bytes, 0, nBytes);
           char[] chars = new char[nChars];
           nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0);
           output += new String(chars, 0, nChars);                                                     
        }
        return output;
    }
}
// The example displays the following output:
//     This is a UTF-8-encoded file that contains primarily Latin text, although it
//     does list the first twelve letters of the Russian (Cyrillic) alphabet:
//     
//     А б в г д е ё ж з и й к
//     
//     The goal is to save this file, then open and decode it as a binary stream.
Imports System.IO
Imports System.Text

Module Example
   Const MAX_BUFFER_SIZE As Integer = 2048
   
   Dim enc8 As Encoding = Encoding.UTF8
      
   Public Sub Main()
      Dim fStream As New FileStream(".\Utf8Example.txt", FileMode.Open)
      Dim contents As String = Nothing
      
      ' If file size is small, read in a single operation.
      If fStream.Length <= MAX_BUFFER_SIZE Then
         Dim bytes(CInt(fStream.Length) - 1) As Byte
         fStream.Read(bytes, 0, bytes.Length)
         contents = enc8.GetString(bytes)
      ' If file size exceeds buffer size, perform multiple reads.
      Else
         contents = ReadFromBuffer(fStream)
      End If
      fStream.Close()
      Console.WriteLine(contents)
   End Sub   

    Private Function ReadFromBuffer(fStream As FileStream) As String
        Dim bytes(MAX_BUFFER_SIZE) As Byte
        Dim output As String = String.Empty
        Dim decoder8 As Decoder = enc8.GetDecoder()
      
        Do While fStream.Position < fStream.Length
           Dim nBytes As Integer = fStream.Read(bytes, 0, bytes.Length)
           Dim nChars As Integer = decoder8.GetCharCount(bytes, 0, nBytes)
           Dim chars(nChars - 1) As Char
           nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0)
           output += New String(chars, 0, nChars)                                                     
        Loop
        Return output
    End Function
End Module
' The example displays the following output:
'     This is a UTF-8-encoded file that contains primarily Latin text, although it
'     does list the first twelve letters of the Russian (Cyrillic) alphabet:
'     
'     ? ? ? ? ? ? ? ? ? ? ? ?
'     
'     The goal is to save this file, then open and decode it as a binary stream.

이 예제에서는 Utf8Example.txt 라는 UTF-8 인코딩된 파일에 저장 해야 하는 다음 텍스트를 사용 합니다.

This is a UTF-8-encoded file that contains primarily Latin text, although it
does list the first twelve letters of the Russian (Cyrillic) alphabet:

А б в г д е ё ж з и й к

The goal is to save this file, then open and decode it as a binary stream.

설명

변환할 데이터를 순차 블록 에서만 사용할 수 있는 경우 (예: 스트림에서 읽은 데이터) 또는 작은 블록으로 분할 해야 하는 데이터 양이 많은 경우 Decoder GetDecoder 파생 클래스의 메서드에서 반환 하는 개체를 사용 해야 합니다.

Encoding.GetChars디코딩 기술 및 고려 사항에 대 한 설명은 참조 항목의 설명 섹션을 참조 하세요.

GetString특정 구현에 대 한 메서드의 정확한 동작은 Encoding 해당 개체에 대해 정의 된 대체 (fallback) 전략에 따라 다릅니다 Encoding . 자세한 내용은 .net에서 문자 인코딩 항목의 "대체 (Fallback) 전략 선택" 섹션을 참조 하세요.

추가 정보

적용 대상

GetString(ReadOnlySpan<Byte>)

파생 클래스에서 재정의할 경우, 지정된 바이트 범위의 모든 바이트를 문자열로 디코딩합니다.

public:
 System::String ^ GetString(ReadOnlySpan<System::Byte> bytes);
public string GetString (ReadOnlySpan<byte> bytes);
member this.GetString : ReadOnlySpan<byte> -> string
Public Function GetString (bytes As ReadOnlySpan(Of Byte)) As String

매개 변수

bytes
ReadOnlySpan<Byte>

유니코드 문자열로 디코딩할 읽기 전용 바이트 범위입니다.

반환

String

제공된 읽기 전용 범위에서 디코딩된 바이트를 포함하는 문자열입니다.

설명

GetString메서드는 성능을 최적화 하도록 디자인 되었습니다. 관리 되는 바이트 배열을 만들고 디코딩하는 대신 중간 개체를 만들지 않고도이 메서드를 호출할 수 있습니다.

변환할 데이터를 순차 블록 에서만 사용할 수 있는 경우 (예: 스트림에서 읽은 데이터) 또는 작은 블록으로 분할 해야 하는 데이터 양이 많은 경우 Decoder GetDecoder 파생 클래스의 메서드에서 반환 하는 개체를 사용 해야 합니다.

Encoding.GetChars디코딩 기술 및 고려 사항에 대 한 설명은 참조 항목의 설명 섹션을 참조 하세요.

GetString특정 구현에 대 한 메서드의 정확한 동작은 Encoding 해당 개체에 대해 정의 된 대체 (fallback) 전략에 따라 다릅니다 Encoding . 자세한 내용은 .net에서 문자 인코딩 항목의 "대체 (Fallback) 전략 선택" 섹션을 참조 하세요.

적용 대상

GetString(Byte*, Int32)

중요

이 API는 CLS 규격이 아닙니다.

파생 클래스에서 재정의할 때 지정된 주소에서 시작하는 지정된 바이트 수를 문자열로 디코딩합니다.

public:
 System::String ^ GetString(System::Byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public string GetString (byte* bytes, int byteCount);
[System.CLSCompliant(false)]
public string GetString (byte* bytes, int byteCount);
public string GetString (byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public string GetString (byte* bytes, int byteCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
member this.GetString : nativeptr<byte> * int -> string
[<System.CLSCompliant(false)>]
member this.GetString : nativeptr<byte> * int -> string
member this.GetString : nativeptr<byte> * int -> string
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.GetString : nativeptr<byte> * int -> string

매개 변수

bytes
Byte*

바이트 배열에 대한 포인터입니다.

byteCount
Int32

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

반환

String

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

특성

예외

bytes이(가) null 포인터입니다.

byteCount가 0보다 작은 경우

대체가 발생했습니다(전체 설명은 .NET의 문자 인코딩 참조).

DecoderFallbackDecoderExceptionFallback로 설정됩니다.

설명

GetString메서드는 바이트 배열에 대 한 네이티브 포인터가 있는 경우 성능을 최적화 하도록 디자인 되었습니다. 관리 되는 바이트 배열을 만들고 디코딩하는 대신 중간 개체를 만들지 않고도이 메서드를 호출할 수 있습니다.

변환할 데이터를 순차 블록 에서만 사용할 수 있는 경우 (예: 스트림에서 읽은 데이터) 또는 작은 블록으로 분할 해야 하는 데이터 양이 많은 경우 Decoder GetDecoder 파생 클래스의 메서드에서 반환 하는 개체를 사용 해야 합니다.

Encoding.GetChars디코딩 기술 및 고려 사항에 대 한 설명은 참조 항목의 설명 섹션을 참조 하세요.

GetString특정 구현에 대 한 메서드의 정확한 동작은 Encoding 해당 개체에 대해 정의 된 대체 (fallback) 전략에 따라 다릅니다 Encoding . 자세한 내용은 .net에서 문자 인코딩 항목의 "대체 (Fallback) 전략 선택" 섹션을 참조 하세요.

추가 정보

적용 대상

GetString(Byte[], Int32, Int32)

파생 클래스에서 재정의되면 지정한 바이트 배열의 바이트 시퀀스를 문자열로 디코딩합니다.

public:
 virtual System::String ^ GetString(cli::array <System::Byte> ^ bytes, int index, int count);
public virtual string GetString (byte[] bytes, int index, int count);
abstract member GetString : byte[] * int * int -> string
override this.GetString : byte[] * int * int -> string
Public Overridable Function GetString (bytes As Byte(), index As Integer, count As Integer) As String

매개 변수

bytes
Byte[]

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

index
Int32

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

count
Int32

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

반환

String

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

예외

바이트 배열에 잘못된 유니코드 코드 포인트가 포함되어 있습니다.

bytes이(가) null인 경우

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

또는

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

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

DecoderFallbackDecoderExceptionFallback로 설정됩니다.

예제

다음 예제에서는 개체가 나타내는 이진 파일에서 u t f-8로 인코딩된 문자열을 읽습니다 FileStream . 2048 바이트 보다 작은 파일의 경우 전체 파일의 내용을 바이트 배열로 읽고 메서드를 호출 GetString(Byte[], Int32, Int32) 하 여 디코딩을 수행 합니다. 큰 파일의 경우 바이트 배열로 2048 바이트를 읽고, 메서드를 호출 Decoder.GetCharCount(Byte[], Int32, Int32) 하 여 배열에 포함 된 문자 수를 확인 한 다음 메서드를 호출 하 여 Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32) 디코딩을 수행 합니다.

using System;
using System.IO;
using System.Text;

public class Example
{
   const int MAX_BUFFER_SIZE = 2048;
   static Encoding enc8 = Encoding.UTF8;
   static byte[] bytes = new byte[MAX_BUFFER_SIZE]; 

   public static void Main()
   {
      FileStream fStream = new FileStream(@".\Utf8Example.txt", FileMode.Open);
      string contents = null;
      
      // If file size is small, read in a single operation.
      if (fStream.Length <= MAX_BUFFER_SIZE) {
         int bytesRead = fStream.Read(bytes, 0, bytes.Length);
         contents = enc8.GetString(bytes, 0, bytesRead);
      }   
      // If file size exceeds buffer size, perform multiple reads.
      else {
         contents = ReadFromBuffer(fStream);
      }
      fStream.Close();
      Console.WriteLine(contents);
   }

    private static string ReadFromBuffer(FileStream fStream)
    {
        string output = String.Empty;
        Decoder decoder8 = enc8.GetDecoder();
      
        while (fStream.Position < fStream.Length) {
           int nBytes = fStream.Read(bytes, 0, bytes.Length);
           int nChars = decoder8.GetCharCount(bytes, 0, nBytes);
           char[] chars = new char[nChars];
           nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0);
           output += new String(chars, 0, nChars);                                                     
        }
        return output;
    }   
}
// The example displays the following output:
//     This is a UTF-8-encoded file that contains primarily Latin text, although it
//     does list the first twelve letters of the Russian (Cyrillic) alphabet:
//     
//     А б в г д е ё ж з и й к
//     
//     The goal is to save this file, then open and decode it as a binary stream.
Imports System.IO
Imports System.Text

Module Example
   Const MAX_BUFFER_SIZE As Integer = 2048
   
   Dim enc8 As Encoding = Encoding.UTF8
   Dim bytes(MAX_BUFFER_SIZE -1) As Byte
      
   Public Sub Main()
      Dim fStream As New FileStream(".\Utf8Example.txt", FileMode.Open)
      Dim contents As String = Nothing
      
      ' If file size is small, read in a single operation.
      If fStream.Length <= MAX_BUFFER_SIZE Then
         
         Dim bytesRead As Integer = fStream.Read(bytes, 0, bytes.Length)
         contents = enc8.GetString(bytes, 0, bytesRead)
      ' If file size exceeds buffer size, perform multiple reads.
      Else
         contents = ReadFromBuffer(fStream)
      End If
      fStream.Close()
      Console.WriteLine(contents)
   End Sub   

    Private Function ReadFromBuffer(fStream As FileStream) As String
        Dim bytes(MAX_BUFFER_SIZE) As Byte
        Dim output As String = String.Empty
        Dim decoder8 As Decoder = enc8.GetDecoder()
      
        Do While fStream.Position < fStream.Length
           Dim nBytes As Integer = fStream.Read(bytes, 0, bytes.Length)
           Dim nChars As Integer = decoder8.GetCharCount(bytes, 0, nBytes)
           Dim chars(nChars - 1) As Char
           nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0)
           output += New String(chars, 0, nChars)                                                     
        Loop
        Return output
    End Function
End Module
' The example displays the following output:
'     This is a UTF-8-encoded file that contains primarily Latin text, although it
'     does list the first twelve letters of the Russian (Cyrillic) alphabet:
'     
'     А б в г д е ё ж з и й к
'     
'     The goal is to save this file, then open and decode it as a binary stream.

이 예제에서는 Utf8Example.txt 라는 UTF-8 인코딩된 파일에 저장 해야 하는 다음 텍스트를 사용 합니다.

This is a UTF-8-encoded file that contains primarily Latin text, although it
does list the first twelve letters of the Russian (Cyrillic) alphabet:

А б в г д е ё ж з и й к

The goal is to save this file, then open and decode it as a binary stream.

설명

변환 되는 데이터를 순차 블록 에서만 사용할 수 있는 경우 (예: 스트림에서 읽은 데이터) 또는 데이터 양이 작은 블록으로 분할 해야 하는 경우에는 Decoder 메서드 또는 메서드에서 제공 하는 또는를 Encoder 파생 된 GetDecoder GetEncoder 클래스의 각 메서드에서 사용 해야 합니다.

Encoding.GetChars디코딩 기술 및 고려 사항에 대 한 설명은 참조 항목의 설명 섹션을 참조 하세요.

추가 정보

적용 대상