This documentation is archived and is not being maintained.

Byte.Parse Method (String, NumberStyles)

Converts the string representation of a number in a specified style to its Byte equivalent.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function Parse ( _
	s As String, _
	style As NumberStyles _
) As Byte
'Usage
Dim s As String
Dim style As NumberStyles
Dim returnValue As Byte

returnValue = Byte.Parse(s, style)
public static byte Parse (
	String s, 
	NumberStyles style
)
public static function Parse (
	s : String, 
	style : NumberStyles
) : byte

Parameters

s

A string containing a number to convert. The string is interpreted using the style specified by style.

style

A bitwise combination of NumberStyles values that indicates the permitted format of s. A typical value to specify is Integer.

Return Value

The Byte value equivalent to the number contained in s.

Exception typeCondition

ArgumentNullException

s is a null reference (Nothing in Visual Basic).

FormatException

s is not of the correct format.

OverflowException

s represents a number less than MinValue or greater than MaxValue.

ArgumentException

style is not a NumberStyles value.

-or-

style is not a combination of AllowHexSpecifier and HexNumber values.

The s parameter is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture.

The s parameter contains a number of the form:

[ws][sign]digits[ws]

Or, if style includes AllowHexSpecifier:

[ws]hexdigits[ws]

Items in square brackets ([ and ]) are optional, and other items are as follows.

ws

Optional white space if permitted by style.

sign

An optional sign.

digits

A sequence of digits from 0 through 9.

hexdigits

A sequence of hexadecimal digits from 0 through f, or 0 through F.

The following code example parses string representations of Byte values with several overloads of the Parse method.

' Example of the Byte.Parse( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module ByteParseDemo
    
    Sub ByteParse( styles As NumberStyles, _
        provider As IFormatProvider )

        Dim byteFormats As String( ) = { _
            " 99 ", " +246 ", " plus246 ", " 1_2_3", " DE " }
            
        ' Parse each string in the byteFormat array, using 
        ' NumberStyles and IFormatProvider, if specified.
        Dim byteString As String
        For Each byteString In  byteFormats

            Dim byteNumber As Byte
                
            ' Display the first part of the output line.
            Console.Write( "  Parse of {0,-15}", _
                String.Format( """{0}""", byteString ) )

            ' Use the appropriate Byte.Parse overload, based on 
            ' the parameters that are specified.
            Try
                If provider Is Nothing Then
                    If styles < 0 Then
                        byteNumber = Byte.Parse( byteString )
                    Else
                        byteNumber = _
                            Byte.Parse( byteString, styles )
                    End If
                ElseIf styles < 0 Then
                    byteNumber = _
                        Byte.Parse( byteString, provider )
                Else
                    byteNumber = _
                        Byte.Parse( byteString, styles, provider )
                End If
                
                ' Display the resulting value if Parse succeeded.
                Console.WriteLine( "succeeded: {0}", _
                    byteNumber )

            ' Display the exception message if Parse failed.
            Catch ex As Exception
                Console.WriteLine( "failed: {0}", ex.Message )
            End Try
        Next byteString
    End Sub 
        
    Sub RunParseDemo( )
            
        ' Do not use IFormatProvider or NumberStyles.
        Console.WriteLine( vbCrLf & _
            "NumberStyles and IFormatProvider are not used:" )
        ByteParse( CType( -1, NumberStyles ), Nothing )
            
        ' Use NumberStyles.HexNumber; do not use IFormatProvider.
        Console.WriteLine( vbCrLf & "NumberStyles." & _
            "HexNumber is used; IFormatProvider is not used:" )
        ByteParse( NumberStyles.HexNumber, Nothing )
            
        ' Get the NumberFormatInfo object from the invariant 
        ' culture, and set the positive-sign string to "plus".
        Dim culture As New CultureInfo( "" )
        Dim numFormat As NumberFormatInfo = culture.NumberFormat
        numFormat.PositiveSign = "plus"
            
        ' Use the NumberFormatInfo object as the IFormatProvider.
        ' Do not use a NumberStyles value.
        Console.WriteLine( vbCrLf & "A NumberStyles value " & _
            "is not used, but the positive sign is ""plus"":" )
        ByteParse( CType( -1, NumberStyles ), numFormat )
            
        ' Change the digit group separator to '_' and the
        ' digit group size to 1.
        numFormat.NumberGroupSeparator = "_"
        numFormat.NumberGroupSizes = New Integer( ) { 1 }
            
        ' Use NumberStyles.Number and the same IFormatProvider.
        Console.WriteLine( vbCrLf & "NumberStyles.Number is " & _
            "used, group separator = ""_"", size = 1:" )
        ByteParse( NumberStyles.Number, numFormat )
    End Sub 
        
    Sub Main( )
        Console.WriteLine( "This example of" & vbCrLf & _
            "  Byte.Parse( string )," & vbCrLf & _
            "  Byte.Parse( string, NumberStyles )," & vbCrLf & _
            "  Byte.Parse( string, IFormatProvider ), and " & _
            vbCrLf & "  Byte.Parse( string, NumberStyles, " & _
            "IFormatProvider )" & vbCrLf & "generates the " & _
            "following output when parsing string representations" & _
            vbCrLf & "of Byte values with each of these " & _
            "forms of Byte.Parse( )." )
            
        RunParseDemo( )

    End Sub 
End Module 

' This example of
'   Byte.Parse( string ),
'   Byte.Parse( string, NumberStyles ),
'   Byte.Parse( string, IFormatProvider ), and
'   Byte.Parse( string, NumberStyles, IFormatProvider )
' generates the following output when parsing string representations
' of Byte values with each of these forms of Byte.Parse( ).
' 
' NumberStyles and IFormatProvider are not used:
'   Parse of " 99 "         succeeded: 99
'   Parse of " +246 "       succeeded: 246
'   Parse of " plus246 "    failed: Input string was not in a correct format.
'   Parse of " 1_2_3"       failed: Input string was not in a correct format.
'   Parse of " DE "         failed: Input string was not in a correct format.
' 
' NumberStyles.HexNumber is used; IFormatProvider is not used:
'   Parse of " 99 "         succeeded: 153
'   Parse of " +246 "       failed: Input string was not in a correct format.
'   Parse of " plus246 "    failed: Input string was not in a correct format.
'   Parse of " 1_2_3"       failed: Input string was not in a correct format.
'   Parse of " DE "         succeeded: 222
' 
' A NumberStyles value is not used, but the positive sign is "plus":
'   Parse of " 99 "         succeeded: 99
'   Parse of " +246 "       failed: Input string was not in a correct format.
'   Parse of " plus246 "    succeeded: 246
'   Parse of " 1_2_3"       failed: Input string was not in a correct format.
'   Parse of " DE "         failed: Input string was not in a correct format.
' 
' NumberStyles.Number is used, group separator = "_", size = 1:
'   Parse of " 99 "         succeeded: 99
'   Parse of " +246 "       failed: Input string was not in a correct format.
'   Parse of " plus246 "    succeeded: 246
'   Parse of " 1_2_3"       succeeded: 123
'   Parse of " DE "         failed: Input string was not in a correct format.

// Example of the Byte.Parse( ) methods.
import System.*;
import System.Globalization.*;

class ByteParseDemo
{
    static void ByteParse(NumberStyles styles, IFormatProvider provider)
    {
        String byteFormats[] =  { " 99 ", " +246 ", " plus246 ", " 1_2_3", 
            " DE " };

        for (int iCtr = 0; iCtr < byteFormats.length; iCtr++) {
            // Parse each string in the byteFormat array, using 
            // NumberStyles and IFormatProvider, if specified.
            ubyte byteNumber;

            // Display the first part of the output line.
            Console.Write("  Parse of {0,-15}", String.Format("\"{0}\"", 
                byteFormats[iCtr]));

            // Use the appropriate Byte.Parse overload, based 
            // on the parameters that are specified.
            try {
                if (provider == null) {
                    if ((int)styles < 0) {
                        byteNumber = System.Byte.Parse(byteFormats[iCtr]);
                    }
                    else {
                        byteNumber = System.Byte.Parse(byteFormats[iCtr], 
                            styles);
                    }
                }
                else {
                    if ((int)styles < 0) {
                        byteNumber = System.Byte.Parse(byteFormats[iCtr], 
                            provider);
                    }
                    else {
                        byteNumber = System.Byte.Parse(byteFormats[iCtr], 
                            styles, provider);
                    }
                }
                // Display the resulting value if Parse succeeded.
                Console.WriteLine("succeeded: {0}", 
                    System.Convert.ToString(byteNumber));
            }
            catch (System.Exception ex) {
                // Display the exception message if Parse failed.
                Console.WriteLine("failed: {0}", ex.get_Message());
            }
            finally { }
        }
    } //ByteParse

    static void RunParseDemo()
    {
        // Do not use IFormatProvider or NumberStyles.
        Console.WriteLine("\nNumberStyles and IFormatProvider are not used:");
        ByteParse(((NumberStyles)(-1)), null);

        // Use NumberStyles.HexNumber; do not use IFormatProvider.
        Console.WriteLine(("\nNumberStyles.HexNumber, "
            + "is used; IFormatProvider is not used:"));
        ByteParse(NumberStyles.HexNumber, null);

        // Get the NumberFormatInfo object from the invariant 
        // culture, and set the positive-sign string to "plus".
        CultureInfo culture = new CultureInfo("");
        NumberFormatInfo numFormat = culture.get_NumberFormat();
        numFormat.set_PositiveSign("plus");

        // Use the NumberFormatInfo object as the IFormatProvider.
        // Do not use a NumberStyles value.
        Console.WriteLine(("\nA NumberStyles value is not used, " 
            + "but the positive sign is \"plus\":"));
        ByteParse(((NumberStyles)(-1)), numFormat);

        // Change the digit group separator to '_' and the
        // digit group size to 1.
        numFormat.set_NumberGroupSeparator("_");
        numFormat.set_NumberGroupSizes(new int[] { 1 });

        // Use NumberStyles.Number and the same IFormatProvider.
        Console.WriteLine(("\nNumberStyles.Number is " 
            + "used, group separator = \"_\", size = 1:"));
        ByteParse(NumberStyles.Number, numFormat);
    } //RunParseDemo

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of\n" + "  Byte.Parse( string ),\n"  
            + "  Byte.Parse( string, NumberStyles ),\n"  
            + "  Byte.Parse( string, IFormatProvider ), and \n"  
            + "  Byte.Parse( string, NumberStyles, IFormatProvider" 
            + " )\ngenerates the following output when parsing "  
            + "string representations\nof Byte values with each "  
            + "of these forms of Byte.Parse( )."));
        RunParseDemo();
    } //main
} //ByteParseDemo

/*
This example of
  Byte.Parse( string ),
  Byte.Parse( string, NumberStyles ),
  Byte.Parse( string, IFormatProvider ), and
  Byte.Parse( string, NumberStyles, IFormatProvider )
generates the following output when parsing string representations
of Byte values with each of these forms of Byte.Parse( ).

NumberStyles and IFormatProvider are not used:
  Parse of " 99 "         succeeded: 99
  Parse of " +246 "       succeeded: 246
  Parse of " plus246 "    failed: Input string was not in a correct format.
  Parse of " 1_2_3"       failed: Input string was not in a correct format.
  Parse of " DE "         failed: Input string was not in a correct format.

NumberStyles.HexNumber, is used; IFormatProvider is not used:
  Parse of " 99 "         succeeded: 153
  Parse of " +246 "       failed: Input string was not in a correct format.
  Parse of " plus246 "    failed: Input string was not in a correct format.
  Parse of " 1_2_3"       failed: Input string was not in a correct format.
  Parse of " DE "         succeeded: 222

A NumberStyles value is not used, but the positive sign is "plus":
  Parse of " 99 "         succeeded: 99
  Parse of " +246 "       failed: Input string was not in a correct format.
  Parse of " plus246 "    succeeded: 246
  Parse of " 1_2_3"       failed: Input string was not in a correct format.
  Parse of " DE "         failed: Input string was not in a correct format.

NumberStyles.Number is used, group separator = "_", size = 1:
  Parse of " 99 "         succeeded: 99
  Parse of " +246 "       failed: Input string was not in a correct format.
  Parse of " plus246 "    succeeded: 246
  Parse of " 1_2_3"       succeeded: 123
  Parse of " DE "         failed: Input string was not in a correct format.
*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: