ASCIIEncoding.GetString 메서드
정의
오버로드
GetString(Byte[]) | |
GetString(Byte[], Int32, Int32) |
바이트 배열의 바이트 범위를 문자열로 디코딩합니다.Decodes a range of bytes from a byte array into a string. |
GetString(Byte[])
GetString(Byte[], Int32, Int32)
바이트 배열의 바이트 범위를 문자열로 디코딩합니다.Decodes a range of bytes from a byte array into a string.
public:
override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount);
public override string GetString (byte[] bytes, int byteIndex, int byteCount);
override this.GetString : byte[] * int * int -> string
Public Overrides Function GetString (bytes As Byte(), byteIndex As Integer, byteCount As Integer) As String
매개 변수
- bytes
- Byte[]
디코딩할 바이트 시퀀스를 포함하는 바이트 배열입니다.The byte array containing the sequence of bytes to decode.
- byteIndex
- Int32
디코딩할 첫 번째 바이트의 인덱스입니다.The index of the first byte to decode.
- byteCount
- Int32
디코딩할 바이트 수입니다.The number of bytes to decode.
반환
지정된 바이트 시퀀스에 대한 디코딩 결과가 포함된 String입니다.A String containing the results of decoding the specified sequence of bytes.
예외
bytes
이(가) null
인 경우bytes
is null
.
index
또는 count
가 0보다 작습니다.index
or count
is less than zero.
또는-or-
index
및 count
가 bytes
에서 올바른 범위를 나타내지 않습니다.index
and count
do not denote a valid range in bytes
.
대체가 발생했습니다(자세한 내용은 .NET의 문자 인코딩 참조).A fallback occurred (for more information, see Character Encoding in .NET) 및-and- DecoderFallback이 DecoderExceptionFallback로 설정됩니다.DecoderFallback is set to DecoderExceptionFallback.
예제
다음 예제에서는 메서드를 사용 하 여 바이트 배열을로 변환 하는 방법을 보여 줍니다 GetString String .The following example demonstrates how to use the GetString method to convert a byte array into a String.
using namespace System;
using namespace System::Text;
int main()
{
// Define a string.
String^ original = "ASCII Encoding Example";
// Instantiate an ASCII encoding object.
ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
// Create an ASCII byte array.
array<Byte>^ bytes = ascii->GetBytes(original);
// Display encoded bytes.
Console::Write("Encoded bytes (in hex): ");
for each (Byte value in bytes)
Console::Write("{0:X2} ", value);
Console::WriteLine();
// Decode the bytes and display the resulting Unicode string.
String^ decoded = ascii->GetString(bytes);
Console::WriteLine("Decoded string: '{0}'", decoded);
}
// The example displays the following output:
// Encoded bytes (in hex): 41 53 43 49 49 20 45 6E 63 6F 64 69 6E 67 20 45 78 61 6D 70 6C 65
// Decoded string: 'ASCII Encoding Example'
using System;
using System.Text;
class Example
{
public static void Main()
{
// Define a string.
String original = "ASCII Encoding Example";
// Instantiate an ASCII encoding object.
ASCIIEncoding ascii = new ASCIIEncoding();
// Create an ASCII byte array.
Byte[] bytes = ascii.GetBytes(original);
// Display encoded bytes.
Console.Write("Encoded bytes (in hex): ");
foreach (var value in bytes)
Console.Write("{0:X2} ", value);
Console.WriteLine();
// Decode the bytes and display the resulting Unicode string.
String decoded = ascii.GetString(bytes);
Console.WriteLine("Decoded string: '{0}'", decoded);
}
}
// The example displays the following output:
// Encoded bytes (in hex): 41 53 43 49 49 20 45 6E 63 6F 64 69 6E 67 20 45 78 61 6D 70 6C 65
// Decoded string: 'ASCII Encoding Example'
Imports System.Text
Module Example
Public Sub Main()
' Define a string.
Dim original As String = "ASCII Encoding Example"
' Instantiate an ASCII encoding object.
Dim ascii As New ASCIIEncoding()
' Create an ASCII byte array.
Dim bytes() As Byte = ascii.GetBytes(original)
' Display encoded bytes.
Console.Write("Encoded bytes (in hex): ")
For Each value In bytes
Console.Write("{0:X2} ", value)
Next
Console.WriteLine()
' Decode the bytes and display the resulting Unicode string.
Dim decoded As String = ascii.GetString(bytes)
Console.WriteLine("Decoded string: '{0}'", decoded)
End Sub
End Module
' The example displays the following output:
' Encoded bytes (in hex): 41 53 43 49 49 20 45 6E 63 6F 64 69 6E 67 20 45 78 61 6D 70 6C 65
' Decoded string: 'ASCII Encoding Example'
설명
스트림에서 읽은 데이터와 같이 변환할 데이터는 순차 블록 에서만 사용할 수 있습니다.Data to be converted, such as data read from a stream, can be available only in sequential blocks. 이 경우 애플리케이션 데이터의 양이 너무 커서 작은 블록으로 나눌 필요가 있는 경우를 사용 해야 합니다 Decoder 또는 Encoder 제공한를 GetDecoder 메서드 또는 GetEncoder 메서드를 각각.In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.
ASCIIEncoding 는 오류 검색을 제공 하지 않습니다.ASCIIEncoding does not provide error detection. 16 진수 0x7F 보다 큰 바이트는 유니코드 물음표 ("?")로 디코딩됩니다.Any byte greater than hexadecimal 0x7F is decoded as the Unicode question mark ("?").
주의
보안상의 이유로 UTF8Encoding , 또는 클래스를 사용 하 UnicodeEncoding UTF32Encoding 고 클래스를 사용 하는 대신 오류 검색을 사용 해야 합니다 ASCIIEncoding .For security reasons, you should use the UTF8Encoding, UnicodeEncoding, or UTF32Encoding classes and enable error detection instead of using the ASCIIEncoding class.
추가 정보
- GetChars(Byte[], Int32, Int32, Char[], Int32)
- GetDecoder()
- GetCharCount(Byte[], Int32, Int32)
- GetMaxCharCount(Int32)