Byte.TryParse 메서드

정의

숫자의 문자열 표현을 해당 Byte로 변환하고, 변환에 성공했는지를 나타내는 값을 반환합니다.

오버로드

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

UTF-8 문자의 범위를 값으로 구문 분석하려고 시도합니다.

TryParse(ReadOnlySpan<Char>, Byte)

숫자의 범위 표현을 해당하는 Byte로 변환하고, 변환이 성공했는지를 나타내는 값을 반환하도록 시도합니다.

TryParse(String, Byte)

숫자의 문자열 표현을 해당 Byte로 변환하고, 변환에 성공했는지를 나타내는 값을 반환합니다.

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

문자 범위를 값으로 구문 분석하려고 시도합니다.

TryParse(String, IFormatProvider, Byte)

문자열을 값으로 구문 분석하려고 시도합니다.

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Byte)

UTF-8 문자의 범위를 값으로 구문 분석하려고 시도합니다.

TryParse(ReadOnlySpan<Byte>, Byte)

숫자의 문자열 표현을 포함하는 UTF-8 문자 범위를 해당하는 8비트 부호 없는 정수로 변환하려고 시도합니다.

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte)

지정된 스타일 및 문화권별 형식으로 된 숫자의 범위 표현을 해당하는 Byte(으)로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.

TryParse(String, NumberStyles, IFormatProvider, Byte)

지정된 스타일 및 문화권별 형식으로 된 숫자의 문자열 표현을 해당하는 Byte로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs

UTF-8 문자의 범위를 값으로 구문 분석하려고 시도합니다.

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = IUtf8SpanParsable<System::Byte>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Byte) As Boolean

매개 변수

utf8Text
ReadOnlySpan<Byte>

구문 분석할 UTF-8 문자의 범위입니다.

provider
IFormatProvider

utf8Text에 대한 문화권별 서식 정보를 제공하는 개체입니다.

result
Byte

반환 시 에는 성공적으로 구문 분석 utf8Text 한 결과 또는 실패 시 정의되지 않은 값이 포함됩니다.

반환

true 성공적으로 구문 분석되었으면 utf8Text 이고, false그렇지 않으면 입니다.

적용 대상

TryParse(ReadOnlySpan<Char>, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

숫자의 범위 표현을 해당하는 Byte로 변환하고, 변환이 성공했는지를 나타내는 값을 반환하도록 시도합니다.

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse (ReadOnlySpan<char> s, out byte result);
static member TryParse : ReadOnlySpan<char> * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Byte) As Boolean

매개 변수

s
ReadOnlySpan<Char>

변환할 숫자를 나타내는 문자를 포함하는 범위입니다.

result
Byte

이 메서드는 변환이 성공한 경우 Byte에 포함된 숫자에 해당하는 s 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 result에 제공된 모든 값을 덮어쓰게 됩니다.

반환

s이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

적용 대상

TryParse(String, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

숫자의 문자열 표현을 해당 Byte로 변환하고, 변환에 성공했는지를 나타내는 값을 반환합니다.

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse (string s, out byte result);
public static bool TryParse (string? s, out byte result);
static member TryParse : string * byte -> bool
Public Shared Function TryParse (s As String, ByRef result As Byte) As Boolean

매개 변수

s
String

변환할 숫자가 포함된 문자열입니다.

result
Byte

이 메서드는 변환이 성공한 경우 Byte에 포함된 숫자에 해당하는 s 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 result에 제공된 모든 값을 덮어쓰게 됩니다.

반환

s이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 여러 문자열 값이 있는 메서드를 호출 TryParse(String, Byte) 합니다.

using namespace System;

void main()
{
   array<String^>^ byteStrings = gcnew array<String^> { nullptr, String::Empty, 
                                                        "1024", "100.1", "100", 
                                                        "+100", "-100", "000000000000000100", 
                                                        "00,100", "   20   ", "FF", "0x1F" };
   Byte byteValue;
   for each (String^ byteString in byteStrings) {
      bool result = Byte::TryParse(byteString, byteValue);
      if (result)
         Console::WriteLine("Converted '{0}' to {1}", 
                            byteString, byteValue);
      else
         Console::WriteLine("Attempted conversion of '{0}' failed.", 
                            byteString);
   }
}
// The example displays the following output:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.`
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.}
using System;

public class ByteConversion
{
   public static void Main()
   {
      string[] byteStrings = { null, string.Empty, "1024",
                               "100.1", "100", "+100", "-100",
                               "000000000000000100", "00,100",
                               "   20   ", "FF", "0x1F" };

      foreach (var byteString in byteStrings)
      {
          CallTryParse(byteString);
      }
   }

   private static void CallTryParse(string stringToConvert)
   {
      byte byteValue;
      bool success = Byte.TryParse(stringToConvert, out byteValue);
      if (success)
      {
         Console.WriteLine("Converted '{0}' to {1}",
                        stringToConvert, byteValue);
      }
      else
      {
         Console.WriteLine("Attempted conversion of '{0}' failed.",
                           stringToConvert);
      }
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.
open System

let callTryParse (stringToConvert: string) =
    match Byte.TryParse stringToConvert with
    | true, byteValue ->
        printfn $"Converted '{stringToConvert}' to {byteValue}"
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."

let byteStrings = 
    [ null; String.Empty; "1024"
      "100.1"; "100"; "+100"; "-100"
      "000000000000000100"; "00,100"
      "   20   "; "FF"; "0x1F" ]

for byteString in byteStrings do
    callTryParse byteString

// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.
Module ByteConversion
   Public Sub Main()
      Dim byteStrings() As String = { Nothing, String.Empty, "1024", 
                                    "100.1", "100", "+100", "-100",
                                    "000000000000000100", "00,100",
                                    "   20   ", "FF", "0x1F"}

      For Each byteString As String In byteStrings
        CallTryParse(byteString)
      Next
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String)  
      Dim byteValue As Byte
      Dim success As Boolean = Byte.TryParse(stringToConvert, byteValue)
      If success Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert)
      End If                        
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '' failed.
'       Attempted conversion of '' failed.
'       Attempted conversion of '1024' failed.
'       Attempted conversion of '100.1' failed.
'       Converted '100' to 100
'       Converted '+100' to 100
'       Attempted conversion of '-100' failed.
'       Converted '000000000000000100' to 100
'       Attempted conversion of '00,100' failed.
'       Converted '   20   ' to 20
'       Attempted conversion of 'FF' failed.
'       Attempted conversion of '0x1F' failed.

설명

변환이 실패하고 매개 변수가 올바른 형식이 아니거나 s 또는 인 경우 null 또는 String.Empty보다 MinValue 작거나 큰 MaxValue숫자를 나타내는 경우 메서드가 를 반환 false 합니다.

Byte.TryParse(String, Byte) 메서드는 변환이 Byte.Parse(String) 실패할 경우 예외를 TryParse(String, Byte) throw하지 않는다는 점을 제외하고 메서드와 유사합니다.

매개 변수는 s 다음 형식의 숫자 문자열 표현이어야 합니다.

[ws][sign]digits[ws]

대괄호 ([ 및 ]) 안의 요소는 선택적 요소입니다. 다음 표에서는 각 요소에 대해 설명합니다.

요소 설명
ws 선택적 공백입니다.
sign 현재 문화권의 속성에 NumberFormatInfo.PositiveSign 지정된 선택적 양수 기호입니다.
숫자 0에서 9까지의 소수 자릿수 시퀀스입니다.

매개 변수는 s 스타일을 사용하여 해석됩니다 Integer . 바이트 값의 10진수 외에도 선행 기호와 함께 선행 및 후행 공백만 허용됩니다. (기호가 있는 경우 양수 기호여야 하거나 메서드가 을 OverflowExceptionthrow합니다.) 에 있을 s수 있는 문화권별 서식 정보와 함께 스타일 요소를 명시적으로 정의하려면 메서드를 Byte.Parse(String, NumberStyles, IFormatProvider) 사용합니다.

매개 변수는 s 현재 문화권에 대한 개체의 서식 정보를 NumberFormatInfo 사용하여 구문 분석됩니다. 자세한 내용은 NumberFormatInfo.CurrentInfo를 참조하세요.

메서드의 이 오버로드는 매개 변수의 Byte.TryParse(String, Byte) 모든 숫자를 s 10진수로 해석합니다. 16진수의 문자열 표현을 구문 분석하려면 오버로드를 호출합니다 Byte.TryParse(String, NumberStyles, IFormatProvider, Byte) .

추가 정보

적용 대상

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

문자 범위를 값으로 구문 분석하려고 시도합니다.

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = ISpanParsable<System::Byte>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Byte) As Boolean

매개 변수

s
ReadOnlySpan<Char>

구문 분석할 문자의 범위입니다.

provider
IFormatProvider

s에 대한 문화권별 서식 정보를 제공하는 개체입니다.

result
Byte

이 메서드가 반환될 때 에는 성공적으로 구문 분석 s한 결과 또는 실패 시 정의되지 않은 값이 포함됩니다.

반환

true 성공적으로 구문 분석되었으면 s 이고, false그렇지 않으면 입니다.

적용 대상

TryParse(String, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

문자열을 값으로 구문 분석하려고 시도합니다.

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = IParsable<System::Byte>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out byte result);
static member TryParse : string * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Byte) As Boolean

매개 변수

s
String

구문 분석할 문자열입니다.

provider
IFormatProvider

s에 대한 문화권별 서식 정보를 제공하는 개체입니다.

result
Byte

이 메서드가 반환될 때 에는 성공적으로 구문 분석 s 한 결과 또는 실패 시 정의되지 않은 값이 포함됩니다.

반환

true 성공적으로 구문 분석되었으면 s 이고, false그렇지 않으면 입니다.

적용 대상

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs

UTF-8 문자의 범위를 값으로 구문 분석하려고 시도합니다.

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean

매개 변수

utf8Text
ReadOnlySpan<Byte>

구문 분석할 UTF-8 문자의 범위입니다.

style
NumberStyles

utf8Text있을 수 있는 숫자 스타일의 비트 조합입니다.

provider
IFormatProvider

utf8Text에 대한 문화권별 서식 정보를 제공하는 개체입니다.

result
Byte

반환 시 에는 성공적으로 구문 분석 utf8Text 한 결과 또는 실패 시 정의되지 않은 값이 포함됩니다.

반환

true 성공적으로 구문 분석되었으면 utf8Text 이고, false그렇지 않으면 입니다.

적용 대상

TryParse(ReadOnlySpan<Byte>, Byte)

Source:
Byte.cs
Source:
Byte.cs

숫자의 문자열 표현을 포함하는 UTF-8 문자 범위를 해당하는 8비트 부호 없는 정수로 변환하려고 시도합니다.

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out byte result);
static member TryParse : ReadOnlySpan<byte> * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Byte) As Boolean

매개 변수

utf8Text
ReadOnlySpan<Byte>

변환할 숫자를 나타내는 UTF-8 문자를 포함하는 범위입니다.

result
Byte

이 메서드는 변환이 성공한 경우 utf8Text에 포함된 숫자에 해당하는 8비트 부호 없는 정수 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 결과에 제공된 모든 값을 덮어쓰게 됩니다.

반환

utf8Text이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

적용 대상

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

지정된 스타일 및 문화권별 형식으로 된 숫자의 범위 표현을 해당하는 Byte(으)로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.

public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result);
public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out byte result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean

매개 변수

s
ReadOnlySpan<Char>

변환할 숫자를 나타내는 문자를 포함하는 범위입니다. 이 범위는 Integer 스타일을 사용하여 해석됩니다.

style
NumberStyles

s에 나타날 수 있는 스타일 요소를 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 Integer입니다.

provider
IFormatProvider

s에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다. providernull이면 현재 스레드 문화권이 사용됩니다.

result
Byte

이 메서드는 변환이 성공한 경우 s에 포함된 숫자에 해당하는 8비트 부호 없는 정수 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 매개 변수가 s 또는 Emptynull 올바른 형식이 아니거나 Byte.MinValue보다 작거나 Byte.MaxValue보다 큰 숫자를 나타내는 경우 변환이 실패합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 result에 제공된 모든 값을 덮어쓰게 됩니다.

반환

s이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

적용 대상

TryParse(String, NumberStyles, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

지정된 스타일 및 문화권별 형식으로 된 숫자의 문자열 표현을 해당하는 Byte로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.

public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result);
public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out byte result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean

매개 변수

s
String

변환할 숫자가 포함된 문자열입니다. 이 문자열은 style이 지정하는 스타일을 사용하여 해석됩니다.

style
NumberStyles

s에 나타날 수 있는 스타일 요소를 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 Integer입니다.

provider
IFormatProvider

s에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다. providernull이면 현재 스레드 문화권이 사용됩니다.

result
Byte

이 메서드는 변환이 성공한 경우 s에 포함된 숫자에 해당하는 8비트 부호 없는 정수 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 매개 변수가 s 또는 Emptynull 올바른 형식이 아니거나 Byte.MinValue보다 작거나 Byte.MaxValue보다 큰 숫자를 나타내는 경우 변환이 실패합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 result에 제공된 모든 값을 덮어쓰게 됩니다.

반환

s이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

예외

styleNumberStyles 값이 아닙니다.

또는

styleAllowHexSpecifierHexNumber 값의 조합이 아닙니다.

예제

다음 예제에서는 여러 문자열 값이 있는 메서드를 호출 TryParse(String, NumberStyles, IFormatProvider, Byte) 합니다.

using namespace System;
using namespace System::Globalization;

void CallTryParse(String^ byteString, NumberStyles styles);

void main()
{
   String^ byteString; 
   NumberStyles styles;

   byteString = "1024";
   styles = NumberStyles::Integer;
   CallTryParse(byteString, styles);

   byteString = "100.1";
   styles = NumberStyles::Integer | NumberStyles::AllowDecimalPoint;
   CallTryParse(byteString, styles);

   byteString = "100.0";
   CallTryParse(byteString, styles);

   byteString = "+100";
   styles = NumberStyles::Integer | NumberStyles::AllowLeadingSign 
            | NumberStyles::AllowTrailingSign;
   CallTryParse(byteString, styles);

   byteString = "-100";
   CallTryParse(byteString, styles);

   byteString = "000000000000000100";
   CallTryParse(byteString, styles);

   byteString = "00,100";
   styles = NumberStyles::Integer | NumberStyles::AllowThousands;
   CallTryParse(byteString, styles);

   byteString = "2E+3   ";
   styles = NumberStyles::Integer | NumberStyles::AllowExponent;
   CallTryParse(byteString, styles);

   byteString = "FF";
   styles = NumberStyles::HexNumber;
   CallTryParse(byteString, styles);

   byteString = "0x1F";
   CallTryParse(byteString, styles);
}

void CallTryParse(String^ stringToConvert, NumberStyles styles)
{  
   Byte byteValue;
   bool result = Byte::TryParse(stringToConvert, styles, 
                                 (IFormatProvider^) nullptr , byteValue);
   if (result)
      Console::WriteLine("Converted '{0}' to {1}", 
                     stringToConvert, byteValue);
   else
      Console::WriteLine("Attempted conversion of '{0}' failed.", 
                        stringToConvert);
}
// The example displays the following output:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.}
using System;
using System.Globalization;

public class ByteConversion2
{
   public static void Main()
   {
      string byteString;
      NumberStyles styles;

      byteString = "1024";
      styles = NumberStyles.Integer;
      CallTryParse(byteString, styles);

      byteString = "100.1";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(byteString, styles);

      byteString = "100.0";
      CallTryParse(byteString, styles);

      byteString = "+100";
      styles = NumberStyles.Integer | NumberStyles.AllowLeadingSign
               | NumberStyles.AllowTrailingSign;
      CallTryParse(byteString, styles);

      byteString = "-100";
      CallTryParse(byteString, styles);

      byteString = "000000000000000100";
      CallTryParse(byteString, styles);

      byteString = "00,100";
      styles = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallTryParse(byteString, styles);

      byteString = "2E+3   ";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(byteString, styles);

      byteString = "FF";
      styles = NumberStyles.HexNumber;
      CallTryParse(byteString, styles);

      byteString = "0x1F";
      CallTryParse(byteString, styles);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      Byte byteValue;
      bool result = Byte.TryParse(stringToConvert, styles,
                                  null as IFormatProvider, out byteValue);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}",
                        stringToConvert, byteValue);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.",
                           stringToConvert.ToString());
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.
open System
open System.Globalization

let callTryParse (stringToConvert: string) (styles: NumberStyles) =
    match Byte.TryParse(stringToConvert, styles, null) with
    | true, byteValue ->
        printfn $"Converted '{stringToConvert}' to {byteValue}"
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."
                        
[<EntryPoint>]
let main _ =
    let byteString = "1024"
    let styles = NumberStyles.Integer
    callTryParse byteString styles

    let byteString = "100.1"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    callTryParse byteString styles

    let byteString = "100.0"
    callTryParse byteString styles

    let byteString = "+100"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign
    callTryParse byteString styles

    let byteString = "-100"
    callTryParse byteString styles

    let byteString = "000000000000000100"
    callTryParse byteString styles

    let byteString = "00,100"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
    callTryParse byteString styles

    let byteString = "2E+3   "
    let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
    callTryParse byteString styles

    let byteString = "FF"
    let styles = NumberStyles.HexNumber
    callTryParse byteString styles

    let byteString = "0x1F"
    callTryParse byteString styles

    0

// The example displays the following output to the console:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.
Imports System.Globalization

Module ByteConversion2
   Public Sub Main()
      Dim byteString As String 
      Dim styles As NumberStyles
      
      byteString = "1024"
      styles = NumberStyles.Integer
      CallTryParse(byteString, styles)
      
      byteString = "100.1"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(byteString, styles)
      
      byteString = "100.0"
      CallTryParse(byteString, styles)
      
      byteString = "+100"
      styles = NumberStyles.Integer Or NumberStyles.AllowLeadingSign _
               Or NumberStyles.AllowTrailingSign
      CallTryParse(byteString, styles)
      
      byteString = "-100"
      CallTryParse(byteString, styles)
      
      byteString = "000000000000000100"
      CallTryParse(byteString, styles)
      
      byteString = "00,100"      
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(byteString, styles)
      
      byteString = "2E+3   "
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(byteString, styles)
      
      byteString = "FF"
      styles = NumberStyles.HexNumber
      CallTryParse(byteString, styles)
      
      byteString = "0x1F"
      CallTryParse(byteString, styles)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles As NumberStyles)  
      Dim byteValue As Byte
      Dim result As Boolean = Byte.TryParse(stringToConvert, styles, Nothing, _
                                            byteValue)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         If stringToConvert Is Nothing Then stringToConvert = ""
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert.ToString())
      End If                        
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '1024' failed.
'       Attempted conversion of '100.1' failed.
'       Converted '100.0' to 100
'       Converted '+100' to 100
'       Attempted conversion of '-100' failed.
'       Converted '000000000000000100' to 100
'       Converted '00,100' to 100
'       Attempted conversion of '2E+3   ' failed.
'       Converted 'FF' to 255
'       Attempted conversion of '0x1F' failed.

설명

메서드는 TryParse 메서드와 Parse 비슷하지만 변환에 TryParse 실패할 경우 메서드가 예외를 throw하지 않습니다.

s 매개 변수는 매개 변수에서 제공하는 개체의 NumberFormatInfo 서식 정보를 사용하여 구문 분석됩니다provider.

style 매개 변수는 구문 분석 작업이 성공하기 위해 매개 변수에 s 허용되는 스타일 요소(예: 공백 또는 양수 기호)를 정의합니다. 열거형의 비트 플래그 NumberStyles 조합이어야 합니다. 의 값 style에 따라 매개 변수에는 s 다음 요소가 포함될 수 있습니다.

[ws] [$] [sign]digits[.fractional_digits][e[sign]digits][ws]

또는 매개 변수에 가 포함된 경우 입니다 styleAllowHexSpecifier.

[ws]hexdigits[ws]

대괄호( [ 및 ] )의 요소는 선택 사항입니다. 다음 표에서는 각 요소에 대해 설명합니다.

요소 설명
ws 선택적 공백입니다. 플래그를 포함하는 경우 styles 시작 부분에 공백이 NumberStyles.AllowLeadingWhite 표시되거나 스타일에 플래그가 포함된 경우 의 끝에 표시할 NumberStyles.AllowTrailingWhite 수 있습니다.
$ 문화권별 통화 기호입니다. 문자열의 위치는 매개 변수의 메서드 provider 에서 반환된 NumberFormatInfo 개체의 속성에 의해 NumberFormatInfo.CurrencyPositivePatternGetFormat 정의됩니다. 플래그가 포함된 경우 style 통화 기호가 NumberStyles.AllowCurrencySymbols 나타날 수 있습니다.
sign 선택적 양수 기호입니다. (에 음수 기호가 있으면 s구문 분석 작업이 실패합니다.) 플래그를 포함하는 경우 의 s 시작 부분에 기호가 표시되거나 플래그가 NumberStyles.AllowLeadingSign 포함된 경우 styles 끝에 표시할 NumberStyles.AllowTrailingSignstyle 있습니다.
숫자 0에서 9까지의 숫자 시퀀스입니다.
. 문화권별 소수점 기호입니다. 에 지정된 provider 문화권의 소수점 기호는 플래그를 포함하는 NumberStyles.AllowDecimalPoint 경우 styles 나타날 수 있습니다.
fractional_digits 숫자 0이 하나 이상 발생합니다. 소수 자릿수는 플래그가 포함된 NumberStyles.AllowDecimalPoint 경우에만 styles 나타날 수 있습니다.
e 값이 지수 표기법으로 표시됨을 나타내는 e 또는 E 문자입니다. 매개 변수는 s 플래그를 포함하는 경우 style 지수 표기법으로 NumberStyles.AllowExponent 숫자를 나타낼 수 있습니다.
hexdigits 0에서 f까지 또는 0부터 F까지의 16진수 숫자 시퀀스입니다.

참고

의 종결 NUL(U+0000) 문자 s 는 인수 값 style 에 관계없이 구문 분석 작업에서 무시됩니다.

10진수만 있는 문자열(스타일에 NumberStyles.None 해당)은 항상 성공적으로 구문 분석됩니다. 나머지 NumberStyles 멤버의 대부분은 이 입력 문자열에 존재할 필요는 없지만 존재할 필요는 없는 요소를 제어합니다. 다음 표에서는 개별 NumberStyles 멤버가 에 s있을 수 있는 요소에 미치는 영향을 나타냅니다.

비 복합 NumberStyles 값 숫자 외에 에서 허용되는 요소
NumberStyles.None 10진수에만 해당합니다.
NumberStyles.AllowDecimalPoint fractional_digits 요소입니다. 그러나 fractional_digits 하나 이상의 숫자로만 구성되어야 합니다. 그렇지 않으면 메서드가 를 반환 false합니다.
NumberStyles.AllowExponent 매개 변수는 s 지수 표기법을 사용할 수도 있습니다. 지수 표기법의 숫자를 나타내는 경우 s 0이 아닌 소수 구성 요소가 없는 데이터 형식 범위 내의 Byte 정수를 나타내야 합니다.
NumberStyles.AllowLeadingWhite 의 시작 부분에 있는 ws 요소입니다 s.
NumberStyles.AllowTrailingWhite 의 끝에 있는 ws 요소입니다 s.
NumberStyles.AllowLeadingSign 양수 기호가 숫자 앞에 나타날 수 있습니다.
NumberStyles.AllowTrailingSign 양수 기호는 숫자 다음에 나타날 수 있습니다.
NumberStyles.AllowParentheses 이 플래그는 지원되지만 메서드는 괄호가 에 s있으면 를 반환 false 합니다.
NumberStyles.AllowThousands 그룹 구분 기호는 에 s표시될 수 있지만 앞에는 0자리 이상의 숫자만 나타날 수 있습니다.
NumberStyles.AllowCurrencySymbol $ 요소입니다.

플래그를 NumberStyles.AllowHexSpecifier 사용하는 s 경우 접두사 없이 16진수 값이어야 합니다. 예를 들어 "F3"은 성공적으로 구문 분석되지만 "0xF3"은 구문 분석하지 않습니다. 에 있을 style 수 있는 유일한 다른 플래그는 및 NumberStyles.AllowTrailingWhite입니다NumberStyles.AllowLeadingWhite. 열거형에는 NumberStyles 공백 플래그를 모두 포함하는 복합 숫자 스타일 NumberStyles.HexNumber이 있습니다.

provider 매개 변수는 IFormatProvider 개체 또는 개체와 같은 구현이며 CultureInfoNumberFormatInfo 메서드는 GetFormat 개체를 NumberFormatInfo 반환합니다. 개체는 NumberFormatInfo 형식 s에 대한 문화권별 정보를 제공합니다.

추가 정보

적용 대상