Export (0) Print
Expand All

Convert.ToString Method (Byte, Int32)

Converts the value of an 8-bit unsigned integer to its equivalent String representation in a specified base.

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

public static string ToString (
	byte value,
	int toBase
)
public static String ToString (
	byte value, 
	int toBase
)
public static function ToString (
	value : byte, 
	toBase : int
) : String

Parameters

value

An 8-bit unsigned integer.

toBase

The base of the return value, which must be 2, 8, 10, or 16.

Return Value

The String representation of value in base toBase.

Exception typeCondition

ArgumentException

toBase is not 2, 8, 10, or 16.

The following code example converts several Byte values to String s with the ToString method in the radixes supported by the method.

// Example of the Convert.ToString( byte, int ) method.
using System;

class ConvertRadixByteDemo
{
    static void RunToStringDemo( )
    {
        byte[ ] values = {
            byte.MinValue, 
            13, 
            byte.MaxValue };
        int[ ]  radices = { 2, 8, 10, 16 };

        // Iterate through the values array.
        foreach( byte value in values )
        {
            // Iterate through the radices.
            foreach( int radix in radices )
            {
                // Convert a value with a radix.
                string valueString = 
                    Convert.ToString( value, radix );

                Console.WriteLine( "{0,7}  {1,3}    {2}", 
                    value, radix, valueString );
            }
        }
    } 

    static void Main( )
    {
        Console.WriteLine(
            "This example of Convert.ToString( byte, int ) " +
            "generates \nthe following output. It converts several " +
            "byte values to \nstrings using the radixes supported " +
            "by the method." );
        Console.WriteLine( 
            "\n  Value  Radix  String" +
            "\n  -----  -----  ------" );

        RunToStringDemo( );
    } 
} 

/*
This example of Convert.ToString( byte, int ) generates
the following output. It converts several byte values to
strings using the radixes supported by the method.

  Value  Radix  String
  -----  -----  ------
      0    2    0
      0    8    0
      0   10    0
      0   16    0
     13    2    1101
     13    8    15
     13   10    13
     13   16    d
    255    2    11111111
    255    8    377
    255   10    255
    255   16    ff
*/

// Example of the Convert.ToString( byte, int ) method.
import System.* ;

class ConvertRadixByteDemo
{
    static void RunToStringDemo() 
    {
        ubyte values[] =  { 0, 13, 255 };
        int radices[] =  { 2, 8, 10, 16 };       
           
        // Iterate through the values array.
        for(int iCtr = 0 ; iCtr < values.length ; iCtr++) {

            ubyte value = (values[iCtr]);
            // Iterate through the radices.
            for(int iCtr1 = 0 ; iCtr1 < radices.length ; iCtr1++) {
                int radix= radices[iCtr1];
         
                // Convert a value with a radix.
                String valueString = Convert.ToString(value, radix);
                Console.WriteLine("{0,7}  {1,3}    {2}",
                    System.Convert.ToString(value),
                    System.Convert.ToString(radix), valueString);
            }
        }
    } //RunToStringDemo   
   
    public static void main(String[] args)
    {   
        Console.WriteLine(("This example of Convert.ToString( byte, int ) " 
            + "generates \nthe following output. It converts several " 
            + "byte values to \nstrings using the radixes supported " 
            + "by the method."));
        Console.WriteLine(("\n  Value  Radix  String"
            + "\n  -----  -----  ------"));
      
        RunToStringDemo();
    } //main
} //ConvertRadixByteDemo

/*
This example of Convert.ToString( byte, int ) generates
the following output. It converts several byte values to
strings using the radixes supported by the method.

  Value  Radix  String
  -----  -----  ------
      0    2    0
      0    8    0
      0   10    0
      0   16    0
     13    2    1101
     13    8    15
     13   10    13
     13   16    d
    255    2    11111111
    255    8    377
    255   10    255
    255   16    ff
*/

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

Community Additions

ADD
Show:
© 2014 Microsoft