Encoding.GetString 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
파생 클래스에서 재정의되는 경우 바이트 시퀀스를 문자열로 디코딩합니다.
오버로드
| Name | Description |
|---|---|
| GetString(Byte[], Int32, Int32) |
파생 클래스에서 재정의되는 경우 지정된 바이트 배열의 바이트 시퀀스를 문자열로 디코딩합니다. |
| GetString(Byte*, Int32) |
파생 클래스에서 재정의되는 경우 지정된 주소에서 시작하는 지정된 바이트 수를 문자열로 디코딩합니다. |
| GetString(Byte[]) |
파생 클래스에서 재정의되는 경우 지정된 바이트 배열의 모든 바이트를 문자열로 디코딩합니다. |
| GetString(ReadOnlySpan<Byte>) |
파생 클래스에서 재정의되는 경우 지정된 바이트 범위의 모든 바이트를 문자열로 디코딩합니다. |
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
디코딩할 바이트 수입니다.
반품
지정된 바이트 시퀀스를 디코딩한 결과를 포함하는 문자열입니다.
예외
바이트 배열에 잘못된 유니코드 코드 포인트가 포함되어 있습니다.
bytes은 null입니다.
예제
다음 예제에서는 개체가 나타내는 이진 파일에서 UTF-8로 인코딩된 문자열을 FileStream 읽습니다. 2,048바이트보다 작은 파일의 경우 전체 파일의 내용을 바이트 배열로 읽고 메서드를 호출 GetString(Byte[], Int32, Int32) 하여 디코딩을 수행합니다. 더 큰 파일의 경우 한 번에 2,048바이트를 바이트 배열로 읽고, 메서드를 호출 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.txtUTF-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 을 참조하세요.
추가 정보
적용 대상
GetString(Byte*, Int32)
Important
이 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)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public string GetString(byte* bytes, int byteCount);
[System.CLSCompliant(false)]
public string GetString(byte* bytes, int byteCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
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
[<System.CLSCompliant(false)>]
member this.GetString : nativeptr<byte> * int -> string
매개 변수
- bytes
- Byte*
바이트 배열에 대한 포인터입니다.
- byteCount
- Int32
디코딩할 바이트 수입니다.
반품
지정된 바이트 시퀀스를 디코딩한 결과를 포함하는 문자열입니다.
- 특성
예외
bytes 은 null 포인터입니다.
byteCount가 0보다 작습니다.
설명
이 GetString 메서드는 바이트 배열에 대한 네이티브 포인터가 있는 경우 성능을 최적화하도록 설계되었습니다. 관리되는 바이트 배열을 만든 다음 디코딩하는 대신 중간 개체를 만들지 않고도 이 메서드를 호출할 수 있습니다.
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나 데이터의 양이 너무 커서 더 작은 블록으로 나누어야 하는 경우 파생 클래스의 메서드에서 GetDecoder 반환된 개체를 사용해야 Decoder 합니다.
디코딩 기술 및 고려 사항에 대한 설명은 참조 항목의 설명 섹션 Encoding.GetChars 을 참조하세요.
특정 Encoding 구현에 대한 메서드의 GetString 정확한 동작은 해당 Encoding 개체에 대해 정의된 대체 전략에 따라 달라집니다. 자세한 내용은 .NET 항목의 "대체 전략 선택" 섹션을 참조하세요.
추가 정보
적용 대상
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[]
디코딩할 바이트 시퀀스를 포함하는 바이트 배열입니다.
반품
지정된 바이트 시퀀스를 디코딩한 결과를 포함하는 문자열입니다.
예외
바이트 배열에 잘못된 유니코드 코드 포인트가 포함되어 있습니다.
bytes은 null입니다.
예제
다음 예제에서는 개체가 나타내는 이진 파일에서 UTF-8로 인코딩된 문자열을 FileStream 읽습니다. 2,048바이트보다 작은 파일의 경우 전체 파일의 내용을 바이트 배열로 읽고 메서드를 호출 GetString(Byte[]) 하여 디코딩을 수행합니다. 더 큰 파일의 경우 한 번에 2,048바이트를 바이트 배열로 읽고, 메서드를 호출 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.txtUTF-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.
설명
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나 데이터의 양이 너무 커서 더 작은 블록으로 나누어야 하는 경우 파생 클래스의 메서드에서 GetDecoder 반환된 개체를 사용해야 Decoder 합니다.
디코딩 기술 및 고려 사항에 대한 설명은 참조 항목의 설명 섹션 Encoding.GetChars 을 참조하세요.
특정 Encoding 구현에 대한 메서드의 GetString 정확한 동작은 해당 Encoding 개체에 대해 정의된 대체 전략에 따라 달라집니다. 자세한 내용은 .NET 항목의 "대체 전략 선택" 섹션을 참조하세요.
추가 정보
적용 대상
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>
유니코드 문자열로 디코딩할 읽기 전용 바이트 범위입니다.
반품
제공된 읽기 전용 범위에서 디코딩된 바이트를 포함하는 문자열입니다.
설명
이 GetString 메서드는 성능을 최적화하도록 설계되었습니다. 관리되는 바이트 배열을 만든 다음 디코딩하는 대신 중간 개체를 만들지 않고도 이 메서드를 호출할 수 있습니다.
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나 데이터의 양이 너무 커서 더 작은 블록으로 나누어야 하는 경우 파생 클래스의 메서드에서 GetDecoder 반환된 개체를 사용해야 Decoder 합니다.
디코딩 기술 및 고려 사항에 대한 설명은 참조 항목의 설명 섹션 Encoding.GetChars 을 참조하세요.
특정 Encoding 구현에 대한 메서드의 GetString 정확한 동작은 해당 Encoding 개체에 대해 정의된 대체 전략에 따라 달라집니다. 자세한 내용은 .NET 항목의 "대체 전략 선택" 섹션을 참조하세요.