Reputation: 70
In which data type can I store a 128-bit data in c#?
For example
dataType bit128 = 340282366920938463463374607431768211455;
What will be the datatype?
Upvotes: 1
Views: 1830
Reputation: 41834
If you have .NET Core 7.0 Preview 5 or newer than you can simply use System.Int128
or System.UInt128
They were implemented in Add support for Int128 and UInt128 data types
For some reason .NET 7 Preview 5 announcement didn't include those types, but in the upcoming .NET 7 Preview 6 announcement there'll also be Int128Converter
and UInt128Converter
for the new types in Preview 5
They didn't have C# support yet though, just like System.Half
, so you'll have to use Int128
explicitly instead of using a native C# keyword
Upvotes: 3
Reputation: 439
You should consider using BigInteger
The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds."
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
Upvotes: 1