System.Convert.ToInt64 方法 (String, Int32)

方法描述

将指定基的数字的字符串表示形式转换为等效的 64 位带符号整数。

语法定义(C# System.Convert.ToInt64 方法 (String, Int32) 的用法)

public static long ToInt64(
	string value,
	int fromBase
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-String 包含要转换的数字的字符串。
fromBase System-Int32 value 中数字的基数,它必须是 2、8、10 或 16。
返回值 System.Int64 与 value 中数字等效的 64 位带符号整数,如果 value 为 null,则为 0(零)。

提示和注释

如果 fromBase 为 16,则可以在 value 参数指定的数字前面加上“0x”或“0X”。

由于不是以 10 为基数的数值表示形式不支持负号,因此 ToInt64(String, Int32) 方法假定负数使用的是 2 的补数表示形式。 换句话说,此方法总是将长整数的最高序二进制位(位 63)解释为其符号位。 因此,可以编写代码以将超出 Int64 数据类型的范围的不是以 10 为基数的数字转换为 Int64 值,从而不让该方法引发异常。 下面的示例将 MaxValue 转换为对应的十六进制字符串表示形式,然后调用 ToInt64(String, Int32) 方法。 该方法将显示消息“0xFFFFFFFFFFFFFFFF converts to -1”(0xFFFFFFFFFFFFFFFF 转换为 -1),而不是引发异常。

C#

VB

复制

// Create a hexadecimal value out of range of the long type.

string value = ulong.MaxValue.ToString("X");

// Use Convert.ToInt64 to convert it back to a number.

try

{

long number = Convert.ToInt64(value, 16);

Console.WriteLine("0x{0} converts to {1}.", value, number);

}

catch (OverflowException)

{

Console.WriteLine("Unable to convert '0x{0}' to a long integer.", value);

}

在执行二元运算或数值转换时,开发人员始终需要负责验证方法是否使用了适当的数值表示形式来解释特定值。 正如下面的示例所演示的那样,可以通过在将一个值转换为对应的十六进制字符串表示形式之前,先确定该值是表示一个无符号类型还是一个有符号类型,从而确保该方法正确地处理溢出。 如果原始值是无符号类型,而转换回整数的结果是一个带有符号位的值,则将引发异常。

C#

VB

复制

// Create a negative hexadecimal value out of range of the Byte type.

ulong sourceNumber = ulong.MaxValue;

bool isSigned = Math.Sign(Convert.ToDouble(sourceNumber.GetType().GetField("MinValue").GetValue(null))) == -1;

string value = sourceNumber.ToString("X");

long targetNumber;

try

{

targetNumber = Convert.ToInt64(value, 16);

if (! isSigned && ((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 a long integer.", value);

}

// Displays the following to the console:

// Unable to convert '0xFFFFFFFFFFFFFFFF' to a long integer.

下面的示例尝试将字符串数组中的每个元素解释为十六进制字符串,并将其转换为长型整数。

C#

VB

复制

using System;

public class Example

{

public static void Main()

{

string[] hexStrings = { "8000000000000000", "0FFFFFFFFFFFFFFF",

"f0000000000001000", "00A30", "D", "-13", "GAD" };

foreach (string hexString in hexStrings)

{

try {

long number = Convert.ToInt64(hexString, 16);

Console.WriteLine("Converted '{0}' to {1:N0}.", hexString, number);

}

catch (FormatException) {

Console.WriteLine("'{0}' is not in the correct format for a hexadecimal number.",

hexString);

}

catch (OverflowException) {

Console.WriteLine("'{0}' is outside the range of an Int64.", hexString);

}

catch (ArgumentException) {

Console.WriteLine("'{0}' is invalid in base 16.", hexString);

}

}

}

}

// The example displays the following output:

// Converted '8000000000000000' to -9,223,372,036,854,775,808.

// Converted '0FFFFFFFFFFFFFFF' to 1,152,921,504,606,846,975.

// 'f0000000000001000' is outside the range of an Int64.

// Converted '00A30' to 2,608.

// Converted 'D' to 13.

// '-13' is invalid in base 16.

// 'GAD' is not in the correct format for a hexadecimal number.

System.Convert.ToInt64 方法 (String, Int32)例子


异常

异常 异常描述
ArgumentException
  • fromBase 不是 2、8、10 或 16。
  • value,它表示一个非 10 为基的有符号数,前面带一个负号。
FormatException value 包含的一个字符不是 fromBase 指定的基中的有效数字。 如果 value 中的第一个字符无效,异常消息则指示没有可转换的数字;否则,该消息将指示 value 包含无效的尾随字符。
OverflowException
  • value,它表示一个非 10 为基的有符号数,前面带一个负号。
  • value 表示小于 Int64.MinValue 或大于 Int64.MaxValue 的数字。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。