gpt4 book ai didi

c# - long.TryParse 在传递十六进制字符串时失败

转载 作者:行者123 更新时间:2023-11-30 19:25:16 27 4
gpt4 key购买 nike

我是 C# 的新手,这个测试用例总是失败。l_sID 的值为 5509df4d0d557e15aca17902。它是一个十六进制值,我正在尝试将它与 long 匹配。我知道它会失败,但我应该怎么做才能通过这个测试?

我尝试使用 BigInteger,但它显示错误。

The type or the namespace name"BigInteger" couldnot be found(are you missing a using directive or assembly refernce when I do BigInteger l_lID = 0; instead of long l_lID =0;

 BigInteger l_lID= 0;
if (!BigInteger.TryParse(l_sID, out l_lID))
throw new Exception("Failed to Upload File ");

最佳答案

5509df4d0d557e15aca17902

此十六进制值是 24 位数字,太大而无法解析为 64 位整数。

供引用:

  • 最大字节0xFF(2位)
  • 最大的 UInt16 0xFFFF(4 位)
  • 最大的 UInt32 0xFFFFFFFF(8 位)
  • 最大的 UInt64 0xFFFFFFFFFFFFFFFF(16 位)

I know it will fail but what should I do pass this test?

如果不更改目标数据类型或输入值,您将无能为力。

如果使用较小的十六进制数,则需要使用 TryParse overload允许您指定数字样式。

bool result = Int64.TryParse(stringToConvert, NumberStyles.HexNumber,
CultureInfo.InvariantCulture, out number);

I tried with BigInteger but it was showing error.

如果您specify the NumberStyle,BigInteger 应该可以在.NET 4.0 及更高版本中正常工作。

bool result = BigInteger.TryParse(hexString,
NumberStyles.AllowHexSpecifier,
null, out number))

The type or the namespace name"BigInteger" couldnot be found(are you missing a using directive or assembly refernce when I do BigInteger l_lID = 0; instead of long l_lID =0

您需要添加对 System.Runtime.Numerics 的程序集引用(在 System.Runtime.Numerics.dll 中)并添加 using System.Numerics;到 C# 文件的顶部。

关于c# - long.TryParse 在传递十六进制字符串时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29951666/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com