将 BigInteger 二进制转换为 BigInteger 数字?
Convert BigInteger Binary to BigInteger Number?
目前我正在使用 Long
整数类型。我使用以下转换 from/to binary/number:
Convert.ToInt64(BinaryString, 2); //Convert binary string of base 2 to number
Convert.ToString(LongNumber, 2); //Convert long number to binary string of base 2
现在我使用的数字已经超过64位,所以开始使用BigInteger。我似乎找不到上面代码的等价物。
如何将超过 64 位的 BinaryString 转换为 BigInteger 数字,反之亦然?
更新:
答案中的引用包含我想要的答案,但我在从数字到二进制的转换中遇到了一些问题。
我使用了第一个参考中提供的以下代码:
public static string ToBinaryString(this BigInteger bigint)
{
var bytes = bigint.ToByteArray();
var idx = bytes.Length - 1;
// Create a StringBuilder having appropriate capacity.
var base2 = new StringBuilder(bytes.Length * 8);
// Convert first byte to binary.
var binary = Convert.ToString(bytes[idx], 2);
// Ensure leading zero exists if value is positive.
if (binary[0] != '0' && bigint.Sign == 1)
{
base2.Append('0');
}
// Append binary string to StringBuilder.
base2.Append(binary);
// Convert remaining bytes adding leading zeros.
for (idx--; idx >= 0; idx--)
{
base2.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
}
return base2.ToString();
}
我得到的结果是错误的:
100001000100000000000100000110000100010000000000000000000000000000000000 ===> 2439583056328331886592
2439583056328331886592 ===> 0100001000100000000000100000110000100010000000000000000000000000000000000
如果将结果二进制字符串放在一起,您会注意到转换是正确的,问题是左边有一个前导零:
100001000100000000000100000110000100010000000000000000000000000000000000
0100001000100000000000100000110000100010000000000000000000000000000000000
我尝试阅读代码中提供的解释并进行更改,但没有成功。
更新 2:
我能够通过更改代码中的以下内容来解决它:
// Ensure leading zero exists if value is positive.
if (binary[0] != '0' && bigint.Sign == 1)
{
base2.Append('0');
// Append binary string to StringBuilder.
base2.Append(binary);
}
MSDN 上有关于 BigIntegers 的很好的参考资料。你能检查一下吗?
https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx
还有一个post可以从二进制转换成双整数Conversion of a binary representation stored in a list of integers (little endian) into a Biginteger
此示例来自 MSDN。
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
遗憾的是,.NET 框架中没有内置任何东西。
幸运的是,Whosebug 社区已经解决了这两个问题:
- BigInteger -> 二进制:BigInteger to Hex/Decimal/Octal/Binary strings?
- 二进制 -> 大整数:C# Convert large binary string to decimal system
目前我正在使用 Long
整数类型。我使用以下转换 from/to binary/number:
Convert.ToInt64(BinaryString, 2); //Convert binary string of base 2 to number
Convert.ToString(LongNumber, 2); //Convert long number to binary string of base 2
现在我使用的数字已经超过64位,所以开始使用BigInteger。我似乎找不到上面代码的等价物。
如何将超过 64 位的 BinaryString 转换为 BigInteger 数字,反之亦然?
更新:
答案中的引用包含我想要的答案,但我在从数字到二进制的转换中遇到了一些问题。
我使用了第一个参考中提供的以下代码:
public static string ToBinaryString(this BigInteger bigint)
{
var bytes = bigint.ToByteArray();
var idx = bytes.Length - 1;
// Create a StringBuilder having appropriate capacity.
var base2 = new StringBuilder(bytes.Length * 8);
// Convert first byte to binary.
var binary = Convert.ToString(bytes[idx], 2);
// Ensure leading zero exists if value is positive.
if (binary[0] != '0' && bigint.Sign == 1)
{
base2.Append('0');
}
// Append binary string to StringBuilder.
base2.Append(binary);
// Convert remaining bytes adding leading zeros.
for (idx--; idx >= 0; idx--)
{
base2.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
}
return base2.ToString();
}
我得到的结果是错误的:
100001000100000000000100000110000100010000000000000000000000000000000000 ===> 2439583056328331886592
2439583056328331886592 ===> 0100001000100000000000100000110000100010000000000000000000000000000000000
如果将结果二进制字符串放在一起,您会注意到转换是正确的,问题是左边有一个前导零:
100001000100000000000100000110000100010000000000000000000000000000000000
0100001000100000000000100000110000100010000000000000000000000000000000000
我尝试阅读代码中提供的解释并进行更改,但没有成功。
更新 2:
我能够通过更改代码中的以下内容来解决它:
// Ensure leading zero exists if value is positive.
if (binary[0] != '0' && bigint.Sign == 1)
{
base2.Append('0');
// Append binary string to StringBuilder.
base2.Append(binary);
}
MSDN 上有关于 BigIntegers 的很好的参考资料。你能检查一下吗? https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx
还有一个post可以从二进制转换成双整数Conversion of a binary representation stored in a list of integers (little endian) into a Biginteger
此示例来自 MSDN。
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
遗憾的是,.NET 框架中没有内置任何东西。
幸运的是,Whosebug 社区已经解决了这两个问题:
- BigInteger -> 二进制:BigInteger to Hex/Decimal/Octal/Binary strings?
- 二进制 -> 大整数:C# Convert large binary string to decimal system