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

方法描述

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

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

[CLSCompliantAttribute(false)]
public static ulong ToUInt64(
	string value,
	int fromBase
)

参数/返回值

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

提示和注释

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

由于 UInt64 数据类型仅支持无符号值,因此 ToUInt64(String, Int32) 方法假定使用无符号二进制表示形式来表示 value。 换句话说,所有 64 位数字都用于表示数值,因而缺少一个符号位。 因此,可以编写代码以将超出 UInt64 数据类型的范围的有符号长整数值转换为 UInt64 值,从而不让该方法引发异常。 下面的示例将 MinValue 转换为对应的十六进制字符串表示形式,然后调用 ToUInt64(String, Int32) 方法。 该方法将显示消息“0x8000000000000000 converts to 9223372036854775808”(0x8000000000000000 转换为 9223372036854775808),而不是引发异常。

C#

VB

复制

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

string value = Convert.ToString(Int64.MinValue, 16);

// Convert it back to a number.

try

{

UInt64 number = Convert.ToUInt64(value, 16);

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

}

catch (OverflowException)

{

Console.WriteLine("Unable to convert '0x{0}' to an unsigned long integer.",

value);

}

在执行二元运算或数值转换时,开发人员始终需要负责验证方法或运算符是否使用了适当的数值表示形式来解释特定值。 下面的示例演示了一种技巧,此技巧可确保在将十六进制字符串转换为 UInt64 值时,所用的方法不会错误地使用二进制表示形式来解释使用 2 的补数表示形式的值。 该示例在将一个值转换为对应的字符串表示形式时,确定该值是表示一个有符号整数还是一个无符号整数。 在此示例将该值转换为 UInt64 值时,它将检查原始值是否为一个有符号整数。 如果原始值是有符号整数并且其高序位已被设置(这表明该原始值是一个负数),则此方法将引发异常。

C#

VB

复制

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

long sourceNumber = Int64.MinValue;

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

string value = Convert.ToString(sourceNumber, 16);

UInt64 targetNumber;

try

{

targetNumber = Convert.ToUInt64(value, 16);

if (isSigned && ((targetNumber & 0x8000000000000000) != 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 unsigned long integer.",

value);

}

// Displays the following to the console:

// Unable to convert '0x8000000000000000' to an unsigned long integer.

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

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

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "80000000", "0FFFFFFF", "F0000000", "00A3000", "D", 
                              "-13", "9AC61", "GAD", "FFFFFFFFFF" };

      foreach (string hexString in hexStrings)
      {
         Console.Write("{0,-12}  -->  ", hexString);
         try {
            uint number = Convert.ToUInt32(hexString, 16);
            Console.WriteLine("{0,18:N0}", number);
         }
         catch (FormatException) {
            Console.WriteLine("{0,18}", "Bad Format");
         }   
         catch (OverflowException)
         {
            Console.WriteLine("{0,18}", "Numeric Overflow");
         }   
         catch (ArgumentException) {
            Console.WriteLine("{0,18}", "Invalid in Base 16");
         }
      }                                            
   }
}
// The example displays the following output:
//       80000000      -->       2,147,483,648
//       0FFFFFFF      -->         268,435,455
//       F0000000      -->       4,026,531,840
//       00A3000       -->             667,648
//       D             -->                  13
//       -13           -->  Invalid in Base 16
//       9AC61         -->             633,953
//       GAD           -->          Bad Format
//       FFFFFFFFFF    -->    Numeric Overflow

异常

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