按一下以給予評分及指教
MSDN
MSDN Library
.NET 開發
.NET Framework
Convert 類別
Convert 方法
ToInt32 方法
 ToInt32 方法 (String, Int32)
本頁僅適用於
Microsoft Visual Studio 2008/.NET Framework 3.5

其他版本也適用於下列軟體:
.NET Framework 類別庫
Convert..::.ToInt32 方法 (String, Int32)

更新:2007 年 11 月

將指定基底中數字的 String 表示,轉換為相等的 32 位元帶正負號的整數。

命名空間:  System
組件:  mscorlib (在 mscorlib.dll 中)
Visual Basic (宣告)
Public Shared Function ToInt32 ( _
    value As String, _
    fromBase As Integer _
) As Integer
Visual Basic (使用方式)
Dim value As String
Dim fromBase As Integer
Dim returnValue As Integer

returnValue = Convert.ToInt32(value, _
    fromBase)
C#
public static int ToInt32(
    string value,
    int fromBase
)
Visual C++
public:
static int ToInt32(
    String^ value, 
    int fromBase
)
J#
public static int ToInt32(
    String value,
    int fromBase
)
JScript
public static function ToInt32(
    value : String, 
    fromBase : int
) : int

參數

value
型別:System..::.String
含有數字的 String
fromBase
型別:System..::.Int32
value 中數字的基底,必須是 2、8、10 或 16。

傳回值

型別:System..::.Int32
32 位元帶正負號的整數,與 value 中的數字相等。
-或-
如果 valuenullNothingnullptrNull 參照 (即 Visual Basic 中的 Nothing),則為零。
例外狀況條件
ArgumentException

fromBase 不是 2、8、10 或 16。

-或-

value,表示非 Base 10 的帶正負號的數字有前置一個負號。

FormatException

value 包含的字元,對於 fromBase 指定的基底而言是無效的數字。如果 value 中的第一個字元無效,則例外狀況訊息會指出無數字可進行轉換,否則訊息會指出 value 包含無效的尾端字元。

OverflowException

value,表示非 Base 10 的帶正負號的數字有前置一個負號。

-或-

傳回值小於 Int32..::.MinValue 或大於 Int32..::.MaxValue

如果 fromBase 為 16,您可以在 value 參數所指定的數字前加上 "0x" 或 "0X"。

因為非 Base 10 的數值表示不支援負號,所以 ToInt32(String, Int32) 方法假設負數使用兩個補數表示。換句話說,方法一定會將整數的最高序位二進位位元 (位元 31) 解譯成正負號位元。因此,您可以撰寫程式碼,讓超出 Int32 資料型別範圍的非 Base 10 的數字轉換成 Int32 值,而不讓方法擲回例外狀況。下列範例會將 Int32..::.MaxValue 加一,將產生的數字轉換成十六進位字串表示,再呼叫 ToInt32(String, Int32) 方法。方法並不會擲回例外狀況,而是會顯示訊息 "0x80000000 converts to -2147483648"。

Visual Basic
' Create a hexadecimal value out of range of the Integer type.
Dim value As String = Convert.ToString(CLng(Integer.MaxValue) + 1, 16)
' Convert it back to a number.
Try
   Dim number As Integer = Convert.ToInt32(value, 16)
   Console.WriteLine("0x{0} converts to {1}.", value, number)
Catch e As OverflowException
   Console.WriteLine("Unable to convert '0x{0}' to an integer.", value)
End Try   

C#
// Create a hexadecimal value out of range of the Integer type.
string value = Convert.ToString((long) int.MaxValue + 1, 16);
// Convert it back to a number.
try
{
   int number = Convert.ToInt32(value, 16);
   Console.WriteLine("0x{0} converts to {1}.", value, number.ToString());
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to an integer.", value);
}   

在執行二進位作業或數值轉換時,開發人員一定要負責驗證方法是否有使用適當的數值表示來轉譯特定值。如下列範例所說明,您可以藉由在將數值轉換為十六進位字串表示前先擷取數值的正負號,以確保方法能適當處理溢位。然後,在原始值為正數時,如果轉換回整數的結果卻為負值時擲回例外狀況。

Visual Basic
' Create a hexadecimal value out of range of the Integer type.
Dim sourceNumber As Long = CLng(Integer.MaxValue) + 1
Dim isNegative As Boolean = (Math.Sign(sourceNumber) = -1)
Dim value As String = Convert.ToString(sourceNumber, 16)
Dim targetNumber As Integer
Try
   targetNumber = Convert.ToInt32(value, 16)
   If Not isNegative And ((targetNumber And &H80000000) <> 0) Then
      Throw New OverflowException()
   Else 
      Console.WriteLine("0x{0} converts to {1}.", value, targetNumber)
   End If    
Catch e As OverflowException
   Console.WriteLine("Unable to convert '0x{0}' to an integer.", value)
End Try 
' Displays the following to the console:
'    Unable to convert '0x80000000' to an integer.     

C#
// Create a hexadecimal value out of range of the Integer type.
long sourceNumber = (long) int.MaxValue + 1;
bool isNegative = Math.Sign(sourceNumber) == -1;
string value = Convert.ToString(sourceNumber, 16);
int targetNumber;
try
{
   targetNumber = Convert.ToInt32(value, 16);
   if (!(isNegative) & (targetNumber & 0x80000000) != 0) 
      throw new OverflowException();
   else 
      Console.WriteLine("0x{0} converts to {1}.", value, targetNumber);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to an integer.", value);
} 
// Displays the following to the console:
//    Unable to convert '80000000' to an integer.     

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求

.NET Framework

支援版本:3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

支援版本:3.5、2.0、1.0

XNA Framework

支援版本:2.0、1.0
社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2008 Microsoft Corporation. 著作權所有,並保留一切權利。 使用規定  |  商標  |  隱私權聲明
Page view tracker