【问题标题】:128-bit data type in C#C# 中的 128 位数据类型
【发布时间】:2022-06-17 21:42:04
【问题描述】:

在c#中我可以将128位数据存储在哪种数据类型中?

例如

dataType bit128 = 340282366920938463463374607431768211455;

数据类型是什么?

【问题讨论】:

标签: c# .net 128-bit


【解决方案1】:

您应该考虑使用 BigInteger

https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?redirectedfrom=MSDN&view=net-6.0

BigInteger 类型是一个不可变类型,它表示一个 理论上没有上下限的任意大整数 界限。”

string positiveString = "91389681247993671255432112000000";
string negativeString = "-90315837410896312071002088037140000";
BigInteger posBigInt = 0;
BigInteger negBigInt = 0;

try {
   posBigInt = BigInteger.Parse(positiveString);
   Console.WriteLine(posBigInt);
}
catch (FormatException)
{
   Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
                     positiveString);
}

if (BigInteger.TryParse(negativeString, out negBigInt))
  Console.WriteLine(negBigInt);
else
   Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
                      negativeString);

// The example displays the following output:
//   9.1389681247993671255432112E+31
//   -9.0315837410896312071002088037E+34

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 2016-02-08
    • 2020-10-31
    • 1970-01-01
    • 2013-08-28
    • 2021-06-13
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多