C# Convert ToString(Byte, Int32)

Description

Convert ToString(Byte, Int32) converts the value of an 8-bit unsigned integer to its equivalent string representation in a specified base.

Syntax

Convert.ToString(Byte, Int32) has the following syntax.


public static string ToString(
  byte value,
  int toBase
)

Parameters

Convert.ToString(Byte, Int32) has the following parameters.

  • value - The 8-bit unsigned integer to convert.
  • toBase - The base of the return value, which must be 2, 8, 10, or 16.

Returns

Convert.ToString(Byte, Int32) method returns The string representation of value in base toBase.

Example

The following example converts each element in a byte array to its equivalent binary, hexadecimal, decimal, and hexadecimal string representations.


/*ww  w.j av  a2 s  . c  om*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    int[] bases = { 2, 8, 10, 16};
    byte[] numbers = { Byte.MinValue, 12, 103, Byte.MaxValue};
    
    foreach (int baseValue in bases)
    {
       Console.WriteLine("Base {0} conversion:", baseValue);
       foreach (byte number in numbers)
       {
          Console.WriteLine("   {0,-5}  -->  0x{1}", 
                            number, Convert.ToString(number, baseValue));
       }
    }

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version