Export (0) Print
Expand All

Byte.ToString Method

Converts the numeric value of this instance to its equivalent string.

Overload List

Converts the numeric value of this instance to its equivalent string.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function ToString() As String
[C#] public override string ToString();
[C++] public: String* ToString();
[JScript] public override function ToString() : String;

Converts the numeric value of this instance to its equivalent string using the specified culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(IFormatProvider) As String
[C#] public virtual string ToString(IFormatProvider);
[C++] public: virtual String* ToString(IFormatProvider*);
[JScript] public function ToString(IFormatProvider) : String;

Converts the numeric value of this instance to its equivalent string using the specified format.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ToString(String) As String
[C#] public string ToString(string);
[C++] public: String* ToString(String*);
[JScript] public function ToString(String) : String;

Converts the numeric value of this instance to its equivalent string using the specified format and culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(String, IFormatProvider) As String
[C#] public virtual string ToString(string, IFormatProvider);
[C++] public: virtual String* ToString(String*, IFormatProvider*);
[JScript] public function ToString(String, IFormatProvider) : String;

Example

[Visual Basic, C#, C++] The following code example formats Byte values with several overloads of the ToString method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToString. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example for the Byte.ToString( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module ByteToStringDemo
    
    Sub RunToStringDemo( )

        Dim smallValue As Byte = 13
        Dim largeValue As Byte = 234
            
        ' Format the Byte values without and with format strings.
        Console.WriteLine( vbCrLf & "IFormatProvider is not used:" )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "No format string:", smallValue.ToString( ), _
            largeValue.ToString( ) )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "'X2' format string:", smallValue.ToString( "X2" ), _
            largeValue.ToString( "X2" ) )
            
        ' Get the NumberFormatInfo object from the 
        ' invariant culture.
        Dim culture As New CultureInfo( "" )
        Dim numInfo As NumberFormatInfo = culture.NumberFormat
            
        ' Set the digit grouping to 1, set the digit separator
        ' to underscore, and set decimal digits to 0.
        numInfo.NumberGroupSizes = New Integer( ) { 1 }
        numInfo.NumberGroupSeparator = "_"
        numInfo.NumberDecimalDigits = 0
            
        ' Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "A NumberFormatInfo object with digit group " & _
            "size = 1 and " & vbCrLf & "digit separator " & _
            "= '_' is used for the IFormatProvider:" )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "No format string:", smallValue.ToString( numInfo ), _
            largeValue.ToString( numInfo ) )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "'N' format string:", _
            smallValue.ToString( "N", numInfo ), _
            largeValue.ToString( "N", numInfo ) )
    End Sub 
        
    Sub Main( )

        Console.WriteLine( "This example of" & vbCrLf & _
            "   Byte.ToString( )," & vbCrLf & _ 
            "   Byte.ToString( String )," & vbCrLf & _
            "   Byte.ToString( IFormatProvider ), and" & vbCrLf & _
            "   Byte.ToString( String, IFormatProvider )" & _
            vbCrLf & "generates the following output when " & _
            "formatting Byte values " & vbCrLf & _
            "with combinations of format " & _
            "strings and IFormatProvider." )
            
        RunToStringDemo( )

    End Sub
End Module 

' This example of
'    Byte.ToString( ),
'    Byte.ToString( String ),
'    Byte.ToString( IFormatProvider ), and
'    Byte.ToString( String, IFormatProvider )
' generates the following output when formatting Byte values
' with combinations of format strings and IFormatProvider.
' 
' IFormatProvider is not used:
'    No format string:           13       234
'    'X2' format string:         0D        EA
'
' A NumberFormatInfo object with digit group size = 1 and
' digit separator = '_' is used for the IFormatProvider:
'    No format string:           13       234
'    'N' format string:         1_3     2_3_4

[C#] 
// Example for the Byte.ToString( ) methods.
using System;
using System.Globalization;

class ByteToStringDemo
{
    static void RunToStringDemo( )
    {
        byte smallValue = 13;
        byte largeValue = 234;
            
        // Format the Byte values without and with format strings.
        Console.WriteLine( "\nIFormatProvider is not used:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "No format string:", smallValue.ToString( ), 
            largeValue.ToString( ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "'X2' format string:", smallValue.ToString( "X2" ), 
            largeValue.ToString( "X2" ) );
            
        // Get the NumberFormatInfo object from the 
        // invariant culture.
        CultureInfo         culture = new CultureInfo( "" );
        NumberFormatInfo    numInfo = culture.NumberFormat;
            
        // Set the digit grouping to 1, set the digit separator
        // to underscore, and set decimal digits to 0.
        numInfo.NumberGroupSizes = new int[ ] { 1 };
        numInfo.NumberGroupSeparator = "_";
        numInfo.NumberDecimalDigits = 0;
            
        // Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine( 
            "\nA NumberFormatInfo object with digit group " +
            "size = 1 and \ndigit separator " +
            "= '_' is used for the IFormatProvider:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "No format string:", smallValue.ToString( numInfo ), 
            largeValue.ToString( numInfo ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "'N' format string:", 
            smallValue.ToString( "N", numInfo ), 
            largeValue.ToString( "N", numInfo ) );
    } 
        
    static void Main( )
    {
        Console.WriteLine( "This example of\n" +
            "   Byte.ToString( ),\n" +
            "   Byte.ToString( String ),\n" + 
            "   Byte.ToString( IFormatProvider ), and\n" +
            "   Byte.ToString( String, IFormatProvider )\n" +
            "generates the following output when formatting " +
            "Byte values \nwith combinations of format " +
            "strings and IFormatProvider." );
            
        RunToStringDemo( );
    }
} 

/*
This example of
   Byte.ToString( ),
   Byte.ToString( String ),
   Byte.ToString( IFormatProvider ), and
   Byte.ToString( String, IFormatProvider )
generates the following output when formatting Byte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:           13       234
   'X2' format string:         0D        EA

A NumberFormatInfo object with digit group size = 1 and
digit separator = '_' is used for the IFormatProvider:
   No format string:           13       234
   'N' format string:         1_3     2_3_4
*/

[C++] 
// Example for the Byte::ToString( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

void RunToStringDemo( )
{    
    Byte smallValue = 13;
    Byte largeValue = 234;

    // Format the Byte values without and with format strings.
    Console::WriteLine( S"\nIFormatProvider is not used:" );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"No format string:", smallValue.ToString( ), 
        largeValue.ToString( ) );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"'X2' format string:", smallValue.ToString( S"X2" ), 
        largeValue.ToString( S"X2" ) );

    // Get the NumberFormatInfo object from the invariant culture.
    CultureInfo*        culture = new CultureInfo( S"" );
    NumberFormatInfo*   numInfo = culture->NumberFormat;

    // Set the digit grouping to 1, set the digit separator
    // to underscore, and set decimal digits to 0.
    Int32 sizes __gc [] = { 1 };
    numInfo->NumberGroupSizes = sizes;
    numInfo->NumberGroupSeparator = S"_";
    numInfo->NumberDecimalDigits = 0;

    // Use the NumberFormatInfo object for an IFormatProvider.
    Console::WriteLine( 
        S"\nA NumberFormatInfo object with digit group " 
        S"size = 1 and \ndigit separator " 
        S"= '_' is used for the IFormatProvider:" );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"No format string:", smallValue.ToString( numInfo ), 
        largeValue.ToString( numInfo ) );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"'N' format string:", smallValue.ToString( S"N", numInfo ), 
        largeValue.ToString( S"N", numInfo ) );
}

void main( )
{    
    Console::WriteLine( 
        S"This example of\n   Byte::ToString( ),\n" 
        S"   Byte::ToString( String* ),\n" 
        S"   Byte::ToString( IFormatProvider* ), and\n" 
        S"   Byte::ToString( String*, IFormatProvider* )\n" 
        S"generates the following output when formatting " 
        S"Byte values \nwith combinations of format " 
        S"strings and IFormatProvider." );
    
    RunToStringDemo( );
}

/*
This example of
   Byte::ToString( ),
   Byte::ToString( String* ),
   Byte::ToString( IFormatProvider* ), and
   Byte::ToString( String*, IFormatProvider* )
generates the following output when formatting Byte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:           13       234
   'X2' format string:         0D        EA

A NumberFormatInfo object with digit group size = 1 and
digit separator = '_' is used for the IFormatProvider:
   No format string:           13       234
   'N' format string:         1_3     2_3_4
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Byte Structure | Byte Members | System Namespace

Show:
© 2014 Microsoft