Export (0) Print
Expand All

Byte.ToString Method ()

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

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

public override string ToString ()
public String ToString ()
public override function ToString () : String

Return Value

The value of this instance.

The return value is formatted with the general format specifier ("G") and the NumberFormatInfo for the current culture.

The following code example formats Byte values with several overloads of the ToString method.

// 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
*/

// Example for the Byte.ToString( ) methods.
import System.* ;
import System.Globalization.* ;

class ByteToStringDemo
{
    static void RunToStringDemo()
    {
        ubyte smallValue = 13;
        ubyte 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:", 
            (UInt16)smallValue, (UInt16)largeValue);
        Console.WriteLine("   {0,-20}{1,10}{2,10}", "'X2' format string:", 
            String.Format("{0:X2}", (UInt16)smallValue), 
            String.Format("{0:X2}", (UInt16)largeValue));

        // Get the NumberFormatInfo object from the 
        // invariant culture.
        CultureInfo culture = new CultureInfo("");
        NumberFormatInfo numInfo = culture.get_NumberFormat();

        // Set the digit grouping to 1, set the digit separator
        // to underscore, and set decimal digits to 0.
        numInfo.set_NumberGroupSizes(new int[] { 1 });
        numInfo.set_NumberGroupSeparator("_");
        numInfo.set_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:",
            System.Convert.ToString(smallValue, numInfo), 
            System.Convert.ToString(largeValue, numInfo));

        //create two object arrays for passing to String.Format()
        Object smallArr[] = { (Int32)smallValue };
        Object largeArr[] = { (Int32)largeValue };

        Console.WriteLine("   {0,-20}{1,10}{2,10}", "'N' format string:", 
            String.Format(numInfo, "{0:N}", smallArr), 
            String.Format(numInfo, "{0:N}", largeArr));
    } //RunToStringDemo

    public static void main(String[] args)
    {
        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();
    } //main
} //ByteToStringDemo

/*
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
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, 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

Community Additions

ADD
Show:
© 2014 Microsoft