Export (0) Print
Expand All

Convert.ToString Method (Byte, Int32)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Shared Function ToString( _
   ByVal value As Byte, _
   ByVal toBase As Integer _
) As String
[C#]
public static string ToString(
 byte value,
 int toBase
);
[C++]
public: static String* ToString(
 unsigned char value,
 int toBase
);
[JScript]
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.

Exceptions

Exception Type Condition
ArgumentException toBase is not 2, 8, 10, or 16.

Example

[Visual Basic, C#, C++] The following code example converts several Byte values to String s with the ToString method in the radixes supported by the method.

[Visual Basic] 
' Example of the Convert.ToString( Byte, Integer ) method.
Imports System
Imports Microsoft.VisualBasic

Module ConvertRadixByteDemo
    
    Sub RunToStringDemo( )

        Dim values as Byte( ) = { _
            Byte.MinValue, _
            13, _
            Byte.MaxValue }
        Dim radices as Integer( ) = { 2, 8, 10, 16 }

        ' Iterate through the values array.
        Dim value as Byte
        For Each value in values

            ' Iterate through the radices.
            Dim radix as Integer
            For Each radix in radices

                ' Convert a value with a radix.
                Dim valueString As String = _
                    Convert.ToString( value, radix )

                Console.WriteLine( "{0,7}  {1,3}    {2}", _
                    value, radix, valueString )
            Next radix
        Next value
    End Sub 

    Sub Main( )

        Console.WriteLine( _
            "This example of Convert.ToString( Byte, Integer ) " & _ 
            "generates " & vbCrLf & "the following output. It " & _
            "converts several Byte values to " & vbCrLf & "strings " & _
            "using the radixes supported by the method." ) 
        Console.WriteLine( vbCrLf & _
            "  Value  Radix  String" & vbCrLf & _
            "  -----  -----  ------" )

        RunToStringDemo( )

    End Sub
End Module 

' This example of Convert.ToString( Byte, Integer ) 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

[C#] 
// 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
*/

[C++] 
// Example of the Convert::ToString( unsigned char, int ) method.
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void RunToStringDemo( )
{
    unsigned char values __gc [ ] = {
        Byte::MinValue, 
        13, 
        Byte::MaxValue };
    int  radices[ 4 ] = { 2, 8, 10, 16 };

    // Implement foreach( unsigned char value in values ).
    IEnumerator*    myEnum = values->GetEnumerator( );
    while( myEnum->MoveNext( ) )
    {
       unsigned char value = Convert::ToByte( myEnum->Current );

        // Iterate through the radices.
        for( int i = 0; i < 4; i++ )
        {
            // Convert a value with a radix.
            int radix = radices[ i ];
            String* valueString = 
                Convert::ToString( value, radix );

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

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

    RunToStringDemo( );
} 

/*
This example of Convert::ToString( unsigned char, int ) generates
the following output. It converts several unsigned char 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
*/

[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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Convert Class | Convert Members | System Namespace | Convert.ToString Overload List

Show:
© 2015 Microsoft