指定した String 形式の数値を、それと等価な 8 ビット符号なし整数に変換します。
Overloads Public Shared Function ToByte( _
ByVal value As String _) As Byte
[C#]
public static byte ToByte(stringvalue);
[C++]
public: static unsigned char ToByte(String* value);
[JScript]
public static function ToByte(
value : String) : Byte;
パラメータ
- value
変換する数値を格納している String 。
戻り値
value の値と等価な 8 ビット符号なし整数。
または
value が null 参照 (Visual Basic では Nothing) の場合は 0。
例外
例外の種類 | 条件 |
---|---|
FormatException | value の構成が、省略可能な符号と、それに続く 0 から 9 までの一連の数字ではありません。 |
OverflowException | value が MinValue 未満の数値か、 MaxValue より大きい数値を表しています。 |
使用例
[Visual Basic, C#, C++] ToByte を使用して、 String の値を Byte に変換する方法については、次のコード例を参照してください。
Public Sub ConvertStringByte(ByVal stringVal As String)
Dim byteVal As Byte = 0
Try
byteVal = System.Convert.ToByte(stringVal)
System.Console.WriteLine("{0} as a byte is: {1}", _
stringVal, byteVal)
Catch exception As System.OverflowException
System.Console.WriteLine( _
"Overflow in string-to-byte conversion.")
Catch exception As System.FormatException
System.Console.WriteLine( _
"The String is not formatted as a Byte.")
Catch exception As System.ArgumentException
System.Console.WriteLine("The String is null.")
End Try
'The conversion from byte to string is always valid.
stringVal = System.Convert.ToString(byteVal)
System.Console.WriteLine("{0} as a string is {1}", _
byteVal, stringVal)
End Sub
[C#]
public void ConvertStringByte(string stringVal) {
byte byteVal = 0;
try {
byteVal = System.Convert.ToByte(stringVal);
System.Console.WriteLine("{0} as a byte is: {1}",
stringVal, byteVal);
}
catch (System.OverflowException) {
System.Console.WriteLine(
"Conversion from string to byte overflowed.");
}
catch (System.FormatException) {
System.Console.WriteLine(
"The string is not formatted as a byte.");
}
catch (System.ArgumentNullException) {
System.Console.WriteLine(
"The string is null.");
}
//The conversion from byte to string is always valid.
stringVal = System.Convert.ToString(byteVal);
System.Console.WriteLine("{0} as a string is {1}",
byteVal, stringVal);
}
[C++]
void ConvertStringByte(String* stringVal)
{
Byte byteVal = 0;
try {
byteVal = System::Convert::ToByte(stringVal);
System::Console::WriteLine(S" {0} as a Byte is: {1}",
stringVal, __box(byteVal));
} catch (System::OverflowException*) {
System::Console::WriteLine(S"Conversion from String* to Byte overflowed.");
} catch (System::FormatException*) {
System::Console::WriteLine(S"The String* is not formatted as a Byte.");
} catch (System::ArgumentNullException*) {
System::Console::WriteLine(S"The String* is 0.");
}
//The conversion from Byte to String* is always valid.
stringVal = System::Convert::ToString(byteVal);
System::Console::WriteLine(S" {0} as a String* is {1}",
__box(byteVal), stringVal);
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
Convert クラス | Convert メンバ | System 名前空間 | Convert.ToByte オーバーロードの一覧 | Byte.Parse