次の方法で共有


Convert.ToSByte メソッド (String)

数値の特定の String 形式を等価の 8 ビット符号付き整数に変換します。

このメソッドは、CLS と互換性がありません。CLS との互換性に関する詳細については 「共通言語仕様の概要」 を参照してください。

<CLSCompliant(False)>
Overloads Public Shared Function ToSByte( _   ByVal value As String _) As SByte
[C#]
[CLSCompliant(false)]
public static sbyte ToSByte(stringvalue);
[C++]
[CLSCompliant(false)]
public: static char ToSByte(String* value);
[JScript]
public
   CLSCompliant(false)
static function ToSByte(value : String) : SByte;

パラメータ

  • value
    変換する数値を格納している String

戻り値

value の値と等価な 8 ビット符号付き整数。

または

value が null 参照 (Visual Basic では Nothing) の場合は 0 です。

例外

例外の種類 条件
FormatException value の構成が、省略可能な符号と、それに続く 0 から 9 までの一連の数字ではありません。
OverflowException value が MinValue 未満の数値か、 MaxValue より大きい数値を表しています。

使用例

[Visual Basic, C#, C++] 既定の書式設定を使用し、 String 形式の SByte (符号付きバイト) 値を ToSByte メソッドで変換するコード例を次に示します。

 
' Example of the Convert.ToSByte( String ) and 
' Convert.ToSByte( String, IFormatProvider ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module ToSByteProviderDemo

    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 ConvertToSByte( numericStr As String, _
        provider As IFormatProvider )

        Dim defaultValue    As Object
        Dim providerValue   As Object

        ' Convert numericStr to SByte without a format provider.
        Try
            defaultValue = Convert.ToSByte( numericStr )
        Catch ex As Exception
            defaultValue = GetExceptionType( ex )
        End Try

        ' Convert numericStr to SByte with a format provider.
        Try
            providerValue = Convert.ToSByte( 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.NegativeSign = "neg "
        provider.PositiveSign = "pos "

        ' These properties do not affect the conversion.
        ' The input string cannot have decimal and group separators.
        provider.NumberDecimalSeparator = "."
        provider.NumberNegativePattern = 0

        Console.WriteLine( "This example of" & vbCrLf & _
            "  Convert.ToSByte( String ) and " & vbCrLf & _
            "  Convert.ToSByte( String, IFormatProvider ) " & _
            vbCrLf & "generates the following output. It " & _
            "converts several strings to " & vbCrLf & "SByte " & _
            "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.
        ConvertToSByte( "123", provider )
        ConvertToSByte( "+123", provider )
        ConvertToSByte( "pos 123", provider )
        ConvertToSByte( "-123", provider )
        ConvertToSByte( "neg 123", provider )
        ConvertToSByte( "123.", provider )
        ConvertToSByte( "(123)", provider )
        ConvertToSByte( "128", provider )
        ConvertToSByte( "-129", provider )
    End Sub 
End Module 

' This example of
'   Convert.ToSByte( String ) and
'   Convert.ToSByte( String, IFormatProvider )
' generates the following output. It converts several strings to
' SByte values, using default formatting or a NumberFormatInfo object.
' 
' String to convert   Default/exception   Provider/exception
' -----------------   -----------------   ------------------
' 123                 123                 123
' +123                123                 FormatException
' pos 123             FormatException     123
' -123                -123                FormatException
' neg 123             FormatException     -123
' 123.                FormatException     FormatException
' (123)               FormatException     FormatException
' 128                 OverflowException   OverflowException
' -129                OverflowException   FormatException

[C#] 
// Example of the Convert.ToSByte( string ) and 
// Convert.ToSByte( string, IFormatProvider ) methods.
using System;
using System.Globalization;

class ToSByteProviderDemo
{
    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 ConvertToSByte( string numericStr, 
        IFormatProvider provider )
    {
        object defaultValue;
        object providerValue;

        // Convert numericStr to SByte without a format provider.
        try
        {
            defaultValue = Convert.ToSByte( numericStr );
        }
        catch( Exception ex )
        {
            defaultValue = GetExceptionType( ex );
        }

        // Convert numericStr to SByte with a format provider.
        try
        {
            providerValue = Convert.ToSByte( 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 = ".";
        provider.NumberNegativePattern = 0;

        Console.WriteLine("This example of\n" +
            "  Convert.ToSByte( string ) and \n" +
            "  Convert.ToSByte( string, IFormatProvider ) " +
            "\ngenerates the following output. It converts " +
            "several strings to \nSByte 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.
        ConvertToSByte( "123", provider );
        ConvertToSByte( "+123", provider );
        ConvertToSByte( "pos 123", provider );
        ConvertToSByte( "-123", provider );
        ConvertToSByte( "neg 123", provider );
        ConvertToSByte( "123.", provider );
        ConvertToSByte( "(123)", provider );
        ConvertToSByte( "128", provider );
        ConvertToSByte( "-129", provider );
    }
}

/*
This example of
  Convert.ToSByte( string ) and
  Convert.ToSByte( string, IFormatProvider )
generates the following output. It converts several strings to
SByte values, using default formatting or a NumberFormatInfo object.

String to convert   Default/exception   Provider/exception
-----------------   -----------------   ------------------
123                 123                 123
+123                123                 FormatException
pos 123             FormatException     123
-123                -123                FormatException
neg 123             FormatException     -123
123.                FormatException     FormatException
(123)               FormatException     FormatException
128                 OverflowException   OverflowException
-129                OverflowException   FormatException
*/ 

[C++] 
// Example of the Convert::ToSByte( String* ) and 
// Convert::ToSByte( 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 ConvertToSByte( String* numericStr, IFormatProvider* provider )
{
    Object* defaultValue;
    Object* providerValue;

    // Convert numericStr to SByte without a format provider.
    try
    {
        defaultValue = __box( Convert::ToSByte( numericStr ) );
    }
    catch( Exception* ex )
    {
        defaultValue = GetExceptionType( ex );
    }

    // Convert numericStr to SByte with a format provider.
    try
    {
        providerValue = __box( Convert::ToSByte( 
            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->NumberNegativePattern = 0;

    Console::WriteLine(S"This example of\n" 
        S"  Convert::ToSByte( String* ) and \n" 
        S"  Convert::ToSByte( String*, IFormatProvider* ) " 
        S"\ngenerates the following output. It converts " 
        S"several strings to \nSByte 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.
    ConvertToSByte( S"123", provider );
    ConvertToSByte( S"+123", provider );
    ConvertToSByte( S"pos 123", provider );
    ConvertToSByte( S"-123", provider );
    ConvertToSByte( S"neg 123", provider );
    ConvertToSByte( S"123.", provider );
    ConvertToSByte( S"(123)", provider );
    ConvertToSByte( S"128", provider );
    ConvertToSByte( S"-129", provider );
}

/*
This example of
  Convert::ToSByte( String* ) and
  Convert::ToSByte( String*, IFormatProvider* )
generates the following output. It converts several strings to
SByte values, using default formatting or a NumberFormatInfo object.

String to convert   Default/exception   Provider/exception
-----------------   -----------------   ------------------
123                 123                 123
+123                123                 FormatException
pos 123             FormatException     123
-123                -123                FormatException
neg 123             FormatException     -123
123.                FormatException     FormatException
(123)               FormatException     FormatException
128                 OverflowException   OverflowException
-129                OverflowException   FormatException
*/ 

[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.ToSByte オーバーロードの一覧 | SByte.Parse