📜  C#| Byte.ToString方法|套装– 1

📅  最后修改于: 2021-05-29 16:24:10             🧑  作者: Mango

此方法用于将当前Byte对象的值转换为其等效的字符串表示形式。 Byte.ToString()方法的重载列表中总共有4种方法,如下所示:

  1. ToString(IFormatProvider)
  2. ToString(String,IFormatProvider)
  3. ToString()
  4. ToString(字符串)

ToString(IFormatProvider)

此方法用于使用指定的区域性特定格式信息将当前Byte对象的数值转换为其等效的字符串表示形式。

句法:

public string ToString (IFormatProvider provider);

参数:此方法采用IFormatProvider类型的对象,该对象提供特定于区域性的格式设置信息。

返回值:该方法以provider参数指定的格式返回此对象的值的字符串表示形式。

下面的程序说明了Byte.ToString(IFormatProvider)方法的用法

范例1:

// C# program to demonstrate
// Byte.ToString(IFormatProvider)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // declaring and initializing bytevalue
        byte byteValue = 15;
  
        // creating and initializing 
        // the object of CultureInfo
        CultureInfo provider = new CultureInfo("en-us");
  
        // getting the value
        // using ToString()
        string value = byteValue.ToString(provider);
  
        // Display the value
        Console.WriteLine("value is {0} and provider is {1}",
                                       value, provider.Name);
    }
}
输出:
value is 15 and provider is en-US

Byte.ToString(String,IFormatProvider)方法

此方法用于使用指定的格式和区域性特定的格式设置信息将当前Byte对象的值转换为其等效的字符串表示形式。

句法:

public string ToString (string format, IFormatProvider provider);

参数:

返回值:该方法以provider参数指定的格式返回此对象的值的字符串表示形式。

例外:如果格式包含不受支持的说明符,则此方法将提供FormatException

下面的程序说明了Byte.ToString(String,IFormatProvider)方法的使用:

范例1:

// C# program to demonstrate the
// Byte.ToString(String, IFormatProvider)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // declaring and initializing bytevalue
        byte byteValue = 15;
  
        // creating and initializing 
        // the object of CultureInfo
        CultureInfo provider = new CultureInfo("fr-FR");
  
        // declaring and initializing format
        string format = "D5";
  
        // getting the value
        // using ToString()
        string value = byteValue.ToString(format, provider);
  
        // Display the value
        Console.WriteLine("value is {0} and provider is {1}",
                                      value, provider.Name);
    }
}
输出:
value is 00015 and provider is fr-FR

示例2:对于FormatException

// C# program to demonstrate the
// Byte.ToString(String, IFormatProvider)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // declaring and initializing bytevalue
            byte byteValue = 15;
  
            // creating and initializing
            // the object of CultureInfo
            CultureInfo provider = new CultureInfo("fr-FR");
  
            // declaring and initializing format
            string format = "s5";
  
            // getting the value
            // using ToString()
            Console.WriteLine("format is invalid");
            string value = byteValue.ToString(format, provider);
  
            // Display the value
            Console.WriteLine("value is {0} and provider is {1}",
                                           value, provider.Name);
        }
        catch (FormatException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
format is invalid
Exception Thrown: System.FormatException

Byte.ToString()方法

此方法用于将当前Byte对象的值转换为其等效的字符串表示形式。

句法:

public override string ToString ();

返回值:该方法返回此对象的值的字符串表示形式,该字符串表示形式由介于0到9之间且无前导零的数字序列组成。

下面的程序说明了Byte.ToString()方法用法

范例1:

// C# program to demonstrate
// Byte.ToString() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // declaring and initializing bytevalue
        byte byteValue = 15;
  
        // getting the value
        // using ToString()
        string value = byteValue.ToString();
  
        // Display the value
        Console.WriteLine("value is {0} ", value);
    }
}
输出:
value is 15

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.byte.tostring?view=netframework-4.7.2