指定した値を 8 ビット符号なし整数に変換します。
オーバーロードの一覧
指定した Boolean 値を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Boolean) As Byte
指定した 8 ビット符号なし整数が返されます。実際の変換は行われません。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Byte) As Byte
指定した Unicode 文字の値を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Char) As Byte
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToByte(DateTime) As Byte
指定した Decimal 数の値を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Decimal) As Byte
指定した倍精度浮動小数点数の値を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Double) As Byte
指定した 16 ビット符号付き整数の値を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Short) As Byte
指定した 32 ビット符号付き整数の値を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Integer) As Byte
指定した 64 ビット符号付き整数の値を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Long) As Byte
指定した Object の値を、8 ビット符号なし整数に変換します。
[Visual Basic] Overloads Public Shared Function ToByte(Object) As Byte
指定した 8 ビット符号付き整数の値を等価の 8 ビット符号なし整数に変換します。このメソッドは、CLS と互換性がありません。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(SByte) As Byte
指定した単精度浮動小数点数の値を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Single) As Byte
指定した String 形式の数値を、それと等価な 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(String) As Byte
指定した 16 ビット符号なし整数の値を等価の 8 ビット符号なし整数に変換します。このメソッドは、CLS と互換性がありません。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(UInt16) As Byte
指定した 32 ビット符号なし整数の値を等価の 8 ビット符号なし整数に変換します。このメソッドは、CLS と互換性がありません。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(UInt32) As Byte
指定した 64 ビット符号なし整数の値を等価の 8 ビット符号なし整数に変換します。このメソッドは、CLS と互換性がありません。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(UInt64) As Byte
[C++] public: static unsigned char ToByte(unsigned __int64);
指定したカルチャに固有の書式情報を使用して、指定した Object の値を 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(Object, IFormatProvider) As Byte
[C++] public: static unsigned char ToByte(Object*, IFormatProvider*);
[JScript] public static function ToByte(Object, IFormatProvider) : Byte;
カルチャに固有の指定した書式情報を使用して、数値の特定の String 形式を等価の 8 ビット符号付き整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(String, IFormatProvider) As Byte
[C++] public: static unsigned char ToByte(String*, IFormatProvider*);
[JScript] public static function ToByte(String, IFormatProvider) : Byte;
指定した基数での数値の String 形式を等価の 8 ビット符号なし整数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToByte(String, Integer) As Byte
[JScript] public static function ToByte(String, int) : Byte;
使用例
[Visual Basic, C#, C++] IFormatProvider オブジェクトを使用し、 String 形式の Byte の符号なし値を ToByte メソッドで変換するコード例を次に示します。
[Visual Basic, C#, C++] メモ ここでは、ToByte のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
' Example of the Convert.ToByte( String ) and
' Convert.ToByte( String, IFormatProvider ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Module ToByteProviderDemo
Dim format As String = "{0,-20}{1,-20}{2}"
' Get the exception type name; remove the namespace prefix.
Function GetExceptionType( ex As Exception ) As String
Dim exceptionType As String = ex.GetType( ).ToString( )
Return exceptionType.Substring( _
exceptionType.LastIndexOf( "."c ) + 1 )
End Function
Sub ConvertToByte( numericStr As String, _
provider As IFormatProvider )
Dim defaultValue As Object
Dim providerValue As Object
' Convert numericStr to Byte without a format provider.
Try
defaultValue = Convert.ToByte( numericStr )
Catch ex As Exception
defaultValue = GetExceptionType( ex )
End Try
' Convert numericStr to Byte with a format provider.
Try
providerValue = Convert.ToByte( numericStr, provider )
Catch ex As Exception
providerValue = GetExceptionType( ex )
End Try
Console.WriteLine( format, numericStr, _
defaultValue, providerValue )
End Sub
Sub Main( )
' Create a NumberFormatInfo object and set several of its
' properties that apply to numbers.
Dim provider As NumberFormatInfo = new NumberFormatInfo( )
' These properties affect the conversion.
provider.PositiveSign = "pos "
provider.NegativeSign = "neg "
' These properties do not affect the conversion.
' The input string cannot have decimal and group separators.
provider.NumberDecimalSeparator = "."
Console.WriteLine( "This example of" & vbCrLf & _
" Convert.ToByte( String ) and " & vbCrLf & _
" Convert.ToByte( String, IFormatProvider ) " & _
vbCrLf & "generates the following output. It " & _
"converts several strings to " & vbCrLf & "Byte " & _
"values, using default formatting " & _
"or a NumberFormatInfo object." & vbCrLf )
Console.WriteLine( format, "String to convert", _
"Default/exception", "Provider/exception" )
Console.WriteLine( format, "-----------------", _
"-----------------", "------------------" )
' Convert strings, with and without an IFormatProvider.
ConvertToByte( "234", provider )
ConvertToByte( "+234", provider )
ConvertToByte( "pos 234", provider )
ConvertToByte( "234.", provider )
ConvertToByte( "255", provider )
ConvertToByte( "256", provider )
ConvertToByte( "-1", provider )
End Sub
End Module
' This example of
' Convert.ToByte( String ) and
' Convert.ToByte( String, IFormatProvider )
' generates the following output. It converts several strings to
' Byte values, using default formatting or a NumberFormatInfo object.
'
' String to convert Default/exception Provider/exception
' ----------------- ----------------- ------------------
' 234 234 234
' +234 234 FormatException
' pos 234 FormatException 234
' 234. FormatException FormatException
' 255 255 255
' 256 OverflowException OverflowException
' -1 OverflowException FormatException
[C#]
// Example of the Convert.ToByte( string ) and
// Convert.ToByte( string, IFormatProvider ) methods.
using System;
using System.Globalization;
class ToByteProviderDemo
{
static string format = "{0,-20}{1,-20}{2}";
// Get the exception type name; remove the namespace prefix.
static string GetExceptionType( Exception ex )
{
string exceptionType = ex.GetType( ).ToString( );
return exceptionType.Substring(
exceptionType.LastIndexOf( '.' ) + 1 );
}
static void ConvertToByte( string numericStr,
IFormatProvider provider )
{
object defaultValue;
object providerValue;
// Convert numericStr to Byte without a format provider.
try
{
defaultValue = Convert.ToByte( numericStr );
}
catch( Exception ex )
{
defaultValue = GetExceptionType( ex );
}
// Convert numericStr to Byte with a format provider.
try
{
providerValue = Convert.ToByte( numericStr, provider );
}
catch( Exception ex )
{
providerValue = GetExceptionType( ex );
}
Console.WriteLine( format, numericStr,
defaultValue, providerValue );
}
public static void Main( )
{
// Create a NumberFormatInfo object and set several of its
// properties that apply to numbers.
NumberFormatInfo provider = new NumberFormatInfo();
// These properties affect the conversion.
provider.NegativeSign = "neg ";
provider.PositiveSign = "pos ";
// These properties do not affect the conversion.
// The input string cannot have decimal and group separators.
provider.NumberDecimalSeparator = ".";
Console.WriteLine("This example of\n" +
" Convert.ToByte( string ) and \n" +
" Convert.ToByte( string, IFormatProvider ) " +
"\ngenerates the following output. It converts " +
"several strings to \nbyte values, using " +
"default formatting or a NumberFormatInfo object.\n" );
Console.WriteLine( format, "String to convert",
"Default/exception", "Provider/exception" );
Console.WriteLine( format, "-----------------",
"-----------------", "------------------" );
// Convert strings, with and without an IFormatProvider.
ConvertToByte( "234", provider );
ConvertToByte( "+234", provider );
ConvertToByte( "pos 234", provider );
ConvertToByte( "234.", provider );
ConvertToByte( "255", provider );
ConvertToByte( "256", provider );
ConvertToByte( "-1", provider );
}
}
/*
This example of
Convert.ToByte( string ) and
Convert.ToByte( string, IFormatProvider )
generates the following output. It converts several strings to
byte values, using default formatting or a NumberFormatInfo object.
String to convert Default/exception Provider/exception
----------------- ----------------- ------------------
234 234 234
+234 234 FormatException
pos 234 FormatException 234
234. FormatException FormatException
255 255 255
256 OverflowException OverflowException
-1 OverflowException FormatException
*/
[C++]
// Example of the Convert::ToByte( String* ) and
// Convert::ToByte( String*, IFormatProvider* ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
const __wchar_t* protoFmt = L"{0,-20}{1,-20}{2}" ;
// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
String* exceptionType = ex->GetType( )->ToString( );
return exceptionType->Substring(
exceptionType->LastIndexOf( '.' ) + 1 );
}
void ConvertToByte( String* numericStr, IFormatProvider* provider )
{
Object* defaultValue;
Object* providerValue;
// Convert numericStr to Byte without a format provider.
try
{
defaultValue = __box( Convert::ToByte( numericStr ) );
}
catch( Exception* ex )
{
defaultValue = GetExceptionType( ex );
}
// Convert numericStr to Byte with a format provider.
try
{
providerValue = __box( Convert::ToByte(
numericStr, provider ) );
}
catch( Exception* ex )
{
providerValue = GetExceptionType( ex );
}
Console::WriteLine( new String( protoFmt ), numericStr,
defaultValue, providerValue );
}
void main( )
{
// Create a NumberFormatInfo object and set several of its
// properties that apply to numbers.
NumberFormatInfo* provider = new NumberFormatInfo( );
// These properties affect the conversion.
provider->NegativeSign = S"neg ";
provider->PositiveSign = S"pos ";
// These properties do not affect the conversion.
// The input string cannot have decimal and group separators.
provider->NumberDecimalSeparator = S".";
provider->NumberGroupSeparator = S",";
Int32 sizes __gc [ ] = { 3 };
provider->NumberGroupSizes = sizes;
Console::WriteLine(S"This example of\n"
S" Convert::ToByte( String* ) and \n"
S" Convert::ToByte( String*, IFormatProvider* ) "
S"\ngenerates the following output. It converts "
S"several strings to \nbyte values, using "
S"default formatting or a NumberFormatInfo object.\n" );
Console::WriteLine( new String( protoFmt ), S"String to convert",
S"Default/exception", S"Provider/exception" );
Console::WriteLine( new String( protoFmt ), S"-----------------",
S"-----------------", S"------------------" );
// Convert strings, with and without an IFormatProvider.
ConvertToByte( S"234", provider );
ConvertToByte( S"+234", provider );
ConvertToByte( S"pos 234", provider );
ConvertToByte( S"234.", provider );
ConvertToByte( S"255", provider );
ConvertToByte( S"256", provider );
ConvertToByte( S"-1", provider );
}
/*
This example of
Convert::ToByte( String* ) and
Convert::ToByte( String*, IFormatProvider* )
generates the following output. It converts several strings to
byte values, using default formatting or a NumberFormatInfo object.
String to convert Default/exception Provider/exception
----------------- ----------------- ------------------
234 234 234
+234 234 FormatException
pos 234 FormatException 234
234. FormatException FormatException
255 255 255
256 OverflowException OverflowException
-1 OverflowException FormatException
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。