C# Convert ToSByte(String, Int32)
Description
Convert ToSByte(String, Int32)
converts the string
representation of a number in a specified base to an equivalent 8-bit signed
integer.
Syntax
Convert.ToSByte(String, Int32)
has the following syntax.
[CLSCompliantAttribute(false)]// w w w. java2 s. co m
public static sbyte ToSByte(
string value,
int fromBase
)
Parameters
Convert.ToSByte(String, Int32)
has the following parameters.
value
- A string that contains the number to convert.fromBase
- The base of the number in value, which must be 2, 8, 10, or 16.
Returns
Convert.ToSByte(String, Int32)
method returns An 8-bit signed integer that is equivalent to the number in value, or 0 (zero)
if value is null.
Example
The following example attempts to interpret the elements in a string array as the binary, octal, and hexadecimal representation of numeric values in order to convert them to unsigned bytes.
using System;/* w ww .java 2 s .c o m*/
public class Example
{
public static void Main()
{
int[] baseValues = { 2, 8, 16};
string[] values = { "FF", "81", "03", "11", "8F", "01", "1C", "111",
"123", "18A" };
// Convert to each supported base.
foreach (int baseValue in baseValues)
{
Console.WriteLine("Converting strings in base {0}:", baseValue);
foreach (string value in values)
{
Console.Write(" '{0,-5} --> ", value + "'");
try {
Console.WriteLine(Convert.ToSByte(value, baseValue));
}
catch (FormatException) {
Console.WriteLine("Bad Format");
}
catch (OverflowException) {
Console.WriteLine("Out of Range");
}
}
Console.WriteLine();
}
}
}
The code above generates the following result.