此方法用于使用指定的区域性特定格式信息将数字的指定字符串表示形式转换为等效的8位无符号整数。
用法:
public static byte ToByte (string value, IFormatProvider provider);
参数:
- value:它是一个包含要转换的数字的字符串。
- provider:它是提供区域性特定格式信息的对象。
返回值:此方法返回一个与value等效的8位无符号整数,如果value为null,则返回零。
异常:
- FormatException:如果该值不包含一个可选的符号,后跟一个数字序列(0到9)。
- OverflowException:如果该值表示一个小于MinValue或大于MaxValue的数字。
以下示例程序旨在说明Convert.ToByte(String,IFormatProvider)方法的用法:
范例1:
// C# program to demonstrate the
// Convert.ToByte() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and intializing String array
string[] values = { "234", "+234", "240",
"255", "140", "120" };
// calling get() Method
Console.WriteLine("Converted bool value "+
"of specified strings:");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
}
catch (FormatException e) {
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e) {
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s, CultureInfo cultures)
{
// converting string to specified bool
byte val = Convert.ToByte(s, cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
}
输出:
Converted bool value of specified strings: 234, 234, 240, 255, 140, 120,
范例2:对于FormatException
// C# program to demonstrate the
// Convert.ToByte() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and intializing String array
string[] values = {"234", "+234", "240",
"255", "140", "A"};
// calling get() Method
Console.WriteLine("]n Converted bool value "+
"of specified strings:");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
}
catch (FormatException e) {
Console.WriteLine("\n\nvalue consist invalid format");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e) {
Console.WriteLine("\n value represents a "+
"number that is less than MinValue");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s,
CultureInfo cultures)
{
// converting string to specified bool
byte val = Convert.ToByte(s, cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
}
输出:
]n Converted bool value of specified strings: 234, 234, 240, 255, 140, value consist invalid format Exception Thrown:System.FormatException
范例3:对于OverflowException
// C# program to demonstrate the
// Convert.ToByte() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and intializing String array
string[] values = {"234", "+234", "240",
"255", "140", "-1" };
// calling get() Method
Console.WriteLine("]n Converted bool value"+
" of specified strings:");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
}
catch (FormatException e) {
Console.WriteLine("\n\nvalue consist invalid format");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e) {
Console.WriteLine("\n\nvalue represents a number"+
" that is less than MinValue");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s, CultureInfo cultures)
{
// converting string to specified bool
byte val = Convert.ToByte(s, cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
}
输出:
]n Converted bool value of specified strings: 234, 234, 240, 255, 140, value represents a number that is less than MinValue Exception Thrown:System.OverflowException
参考:
相关用法
- C# Boolean.ToString(IFormatProvider)用法及代码示例
- C# Convert.ToInt32(String, IFormatProvider)用法及代码示例
- C# Convert.ToUInt16(String, IFormatProvider)用法及代码示例
- C# Convert.ToSByte(String, IFormatProvider)用法及代码示例
- C# Convert.ToInt64(String, IFormatProvider)用法及代码示例
- C# Convert.ToString(String, IFormatProvider)用法及代码示例
- C# Convert.ToSingle(String, IFormatProvider)用法及代码示例
- C# Convert.ToInt16(String, IFormatProvider)用法及代码示例
- C# Convert.ToUInt32(String, IFormatProvider)用法及代码示例
- C# Convert.ToDouble(String, IFormatProvider)用法及代码示例
- C# Convert.ToDecimal(String, IFormatProvider)用法及代码示例
- C# Convert.ToDateTime(String, IFormatProvider)用法及代码示例
- C# Convert.ToUInt64(String, IFormatProvider)用法及代码示例
- C# Convert.ToBoolean(String, IFormatProvider)用法及代码示例
- C# Convert.ToChar(String, IFormatProvider)用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Convert.ToByte(String, IFormatProvider) Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。