sno*_*yak 12 c# tostring biginteger
在Java中,我能做到
BigInteger b = new BigInteger(500);
Run Code Online (Sandbox Code Playgroud)
然后按照我的喜好格式化它
b.toString(2); //binary
b.toString(8); //octal
b.toString(10); //decimal
b.toString(16); //hexadecimal
Run Code Online (Sandbox Code Playgroud)
在C#中,我可以做到
int num = int.Parse(b.ToString());
Convert.ToString(num,2) //binary
Convert.ToString(num,8) //octal
Run Code Online (Sandbox Code Playgroud)
但我只能用long价值观做得更小.是否有一些方法可以打印具有指定基数的BigInteger?我发布了这个,BigInteger Parse Octal String?,昨天收到了如何将基本上所有字符串转换为BigInteger值的解决方案,但是没有成功输出.
Kev*_*ice 32
BigInteger为十进制,十六进制,二进制,八进制字符串:让我们从一个BigInteger值开始:
BigInteger bigint = BigInteger.Parse("123456789012345678901234567890");
Run Code Online (Sandbox Code Playgroud)
内置Base 10(十进制)和base 16(十六进制)覆盖很容易:
// Convert to base 10 (decimal):
string base10 = bigint.ToString();
// Convert to base 16 (hexadecimal):
string base16 = bigint.ToString("X");
Run Code Online (Sandbox Code Playgroud)
请注意,ToString("X")当值为BigInteger正时,确保十六进制字符串具有前导零.这与ToString("X")从抑制前导零的其他值类型转换时的通常行为不同.
例:
var positiveBigInt = new BigInteger(128);
var negativeBigInt = new BigInteger(-128);
Console.WriteLine(positiveBigInt.ToString("X"));
Console.WriteLine(negativeBigInt.ToString("X"));
Run Code Online (Sandbox Code Playgroud)
结果:
080
80
Run Code Online (Sandbox Code Playgroud)
这种行为有一个目的,因为前导零表示BigInteger是正值 - 基本上,前导零提供符号.这是必要的(与其他值类型转换相反),因为a BigInteger没有固定大小; 因此,没有指定的符号位.前导零表示正值,而不是负值.这允许BigInteger通过ToString()和返回"往返" 值Parse().在MSDN 上的BigInteger Structure页面上讨论了此行为.
这是一个包含扩展方法的类,用于将BigInteger实例转换为二进制,十六进制和八进制字符串:
using System;
using System.Numerics;
using System.Text;
/// <summary>
/// Extension methods to convert <see cref="System.Numerics.BigInteger"/>
/// instances to hexadecimal, octal, and binary strings.
/// </summary>
public static class BigIntegerExtensions
{
/// <summary>
/// Converts a <see cref="BigInteger"/> to a binary string.
/// </summary>
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
/// <returns>
/// A <see cref="System.String"/> containing a binary
/// representation of the supplied <see cref="BigInteger"/>.
/// </returns>
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();
}
/// <summary>
/// Converts a <see cref="BigInteger"/> to a hexadecimal string.
/// </summary>
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
/// <returns>
/// A <see cref="System.String"/> containing a hexadecimal
/// representation of the supplied <see cref="BigInteger"/>.
/// </returns>
public static string ToHexadecimalString(this BigInteger bigint)
{
return bigint.ToString("X");
}
/// <summary>
/// Converts a <see cref="BigInteger"/> to a octal string.
/// </summary>
/// <param name="bigint">A <see cref="BigInteger"/>.</param>
/// <returns>
/// A <see cref="System.String"/> containing an octal
/// representation of the supplied <see cref="BigInteger"/>.
/// </returns>
public static string ToOctalString(this BigInteger bigint)
{
var bytes = bigint.ToByteArray();
var idx = bytes.Length - 1;
// Create a StringBuilder having appropriate capacity.
var base8 = new StringBuilder(((bytes.Length / 3) + 1) * 8);
// Calculate how many bytes are extra when byte array is split
// into three-byte (24-bit) chunks.
var extra = bytes.Length % 3;
// If no bytes are extra, use three bytes for first chunk.
if (extra == 0)
{
extra = 3;
}
// Convert first chunk (24-bits) to integer value.
int int24 = 0;
for (; extra != 0; extra--)
{
int24 <<= 8;
int24 += bytes[idx--];
}
// Convert 24-bit integer to octal without adding leading zeros.
var octal = Convert.ToString(int24, 8);
// Ensure leading zero exists if value is positive.
if (octal[0] != '0' && bigint.Sign == 1)
{
base8.Append('0');
}
// Append first converted chunk to StringBuilder.
base8.Append(octal);
// Convert remaining 24-bit chunks, adding leading zeros.
for (; idx >= 0; idx -= 3)
{
int24 = (bytes[idx] << 16) + (bytes[idx - 1] << 8) + bytes[idx - 2];
base8.Append(Convert.ToString(int24, 8).PadLeft(8, '0'));
}
return base8.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
乍一看,这些方法似乎比必要的更复杂.实际上,添加了一些额外的复杂性以确保转换后的字符串中存在正确的前导零.
让我们检查每个扩展方法,看看它们是如何工作的:
以下是如何使用此扩展方法将a转换BigInteger为二进制字符串:
// Convert BigInteger to binary string.
bigint.ToBinaryString();
Run Code Online (Sandbox Code Playgroud)
每种扩展方法的基本核心是BigInteger.ToByteArray()方法.此方法将a转换BigInteger为字节数组,这是我们如何获取BigInteger值的二进制表示形式:
var bytes = bigint.ToByteArray();
Run Code Online (Sandbox Code Playgroud)
但要注意,返回的字节数组是小端序,因此第一个数组元素是最低有效字节(LSB)BigInteger.由于a StringBuilder用于构建输出字符串 - 从最高有效位(MSB)开始 - 必须反向迭代字节数组,以便首先转换最高有效字节.
因此,索引指针被设置为字节数组中的最高有效数字(最后一个元素):
var idx = bytes.Length - 1;
Run Code Online (Sandbox Code Playgroud)
要捕获转换后的字节,需要StringBuilder创建一个:
var base2 = new StringBuilder(bytes.Length * 8);
Run Code Online (Sandbox Code Playgroud)
该StringBuilder构造函数采用了容量StringBuilder.StringBuilder通过将要转换的字节数乘以8(每个转换的字节产生八个二进制数字)来计算所需的容量.
然后将第一个字节转换为二进制字符串:
var binary = Convert.ToString(bytes[idx], 2);
Run Code Online (Sandbox Code Playgroud)
此时,如果BigInteger是正值,则必须确保存在前导零(参见上面的讨论).如果第一个转换的数字不是零,并且bigint是正数,则a '0'附加到StringBuilder:
// Ensure leading zero exists if value is positive.
if (binary[0] != '0' && bigint.Sign == 1)
{
base2.Append('0');
}
Run Code Online (Sandbox Code Playgroud)
接下来,转换后的字节将附加到StringBuilder:
base2.Append(binary);
Run Code Online (Sandbox Code Playgroud)
要转换剩余的字节,循环以相反的顺序迭代字节数组的其余部分:
for (idx--; idx >= 0; idx--)
{
base16.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
}
Run Code Online (Sandbox Code Playgroud)
请注意,根据需要,每个转换后的字节在左侧用零填充('0'),以便转换后的字符串为八个二进制字符.这非常重要.如果没有这个填充,十六进制值'101'将被转换为二进制值'11'.前导零确保转换为"100000001".
转换所有字节时,StringBuilder包含完整的二进制字符串,由扩展方法返回:
return base2.ToString();
Run Code Online (Sandbox Code Playgroud)
将a转换BigInteger为八进制(基数为8)字符串更复杂.问题是八进制数字代表三位,它不是由创建的字节数组的每个元素中保存的八位的偶数倍BigInteger.ToByteArray().为了解决这个问题,来自阵列的三个字节被组合成24位的块.每个24位块均匀转换为八个八进制字符.
第一个24位块需要一些模数学:
var extra = bytes.Length % 3;
Run Code Online (Sandbox Code Playgroud)
当整个字节数组被分成三字节(24位)块时,此计算确定多少字节是"额外的".第一次转换为八进制(最高有效位)获得"额外"字节,以便所有剩余的转换将分别获得三个字节.
如果没有"额外"字节,则第一个块获得完整的三个字节:
if (extra == 0)
{
extra = 3;
}
Run Code Online (Sandbox Code Playgroud)
第一个块被加载到一个整数变量中int24,该变量最多可容纳24位.块的每个字节都被加载.当加载额外的字节时,先前的位int24被左移8位以腾出空间:
int int24 = 0;
for (; extra != 0; extra--)
{
int24 <<= 8;
int24 += bytes[idx--];
}
Run Code Online (Sandbox Code Playgroud)
通过以下方式将24位块转换为八进制:
var octal = Convert.ToString(int24, 8);
Run Code Online (Sandbox Code Playgroud)
同样,如果BigInteger是正值,则第一个数字必须是前导零:
// Ensure leading zero exists if value is positive.
if (octal[0] != '0' && bigint.Sign == 1)
{
base8.Append('0');
}
Run Code Online (Sandbox Code Playgroud)
第一个转换的块附加到StringBuilder:
base8.Append(octal);
Run Code Online (Sandbox Code Playgroud)
其余的24位块在循环中转换:
for (; idx >= 0; idx -= 3)
{
int24 = (bytes[idx] << 16) + (bytes[idx -1] << 8) + bytes[idx - 2];
base8.Append(Convert.ToString(int24, 8).PadLeft(8, '0'));
}
Run Code Online (Sandbox Code Playgroud)
与二进制转换一样,每个转换的八进制字符串都用零填充,以便'7'变为'00000007'.这样可以确保不会从转换后的字符串中间删除零(即"17"而不是"100000007").
将a转换BigInteger为其他数字基数可能要复杂得多.只要数字基数是2的幂(即,2,4,8,16),由其创建的字节数组BigInteger.ToByteArray()可以适当地分成比特块并被转换.
但是,如果数字基数不是2的幂,则问题变得更加复杂并且需要大量的循环和除法.由于这种数字基数转换很少,我只在这里介绍了流行的计算数字基数.