Biginteger to Hex/Decimal/Octal/Binary Strings

BigInteger to Hex/Decimal/Octal/Binary strings?

Convert BigInteger to decimal, hex, binary, octal string:

Let's start with a BigInteger value:

BigInteger bigint = BigInteger.Parse("123456789012345678901234567890");

Base 10 and Base 16

The built-in Base 10 (decimal) and base 16 (hexadecimal) conversions are easy:

// Convert to base 10 (decimal):
string base10 = bigint.ToString();

// Convert to base 16 (hexadecimal):
string base16 = bigint.ToString("X");

Leading Zeros (positive vs. negative BigInteger values)

Take note, that ToString("X") ensures hexadecimal strings have a leading zero when the value of BigInteger is positive. This is unlike the usual behavior of ToString("X") when converting from other value types where leading zeros are suppressed.

EXAMPLE:

var positiveBigInt = new BigInteger(128);
var negativeBigInt = new BigInteger(-128);
Console.WriteLine(positiveBigInt.ToString("X"));
Console.WriteLine(negativeBigInt.ToString("X"));

RESULT:

080
80

There is a purpose for this behavior as a leading zero indicates the BigInteger is a positive value--essentially, the leading zero provides the sign. This is necessary (as opposed to other value type conversions) because a BigInteger has no fixed size; therefore, there is no designated sign bit. The leading zero identifies a positive value, as opposed to a negative one. This allows for "round-tripping" BigInteger values out through ToString() and back in through Parse(). This behavior is discussed on the BigInteger Structure page on MSDN.

Extension methods: BigInteger to Binary, Hex, and Octal

Here is a class containing extension methods to convert BigInteger instances to binary, hexadecimal, and octal strings:

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();
}
}

On first glance, these methods may seem more complex than necessary. A bit of extra complexity is, indeed, added to ensure the proper leading zeros are present in the converted strings.

Let's examine each extension method to see how they work:

BigInteger.ToBinaryString()

Here is how to use this extension method to convert a BigInteger to a binary string:

// Convert BigInteger to binary string.
bigint.ToBinaryString();

The fundamental core of each of these extension methods is the BigInteger.ToByteArray() method. This method converts a BigInteger to a byte array, which is how we can get the binary representation of a BigInteger value:

var bytes = bigint.ToByteArray();

Beware, though, the returned byte array is in little-endian order, so the first array element is the least-significant byte (LSB) of the BigInteger. Since a StringBuilder is used to build the output string--which starts at the most-significant digit (MSB)--the byte array must be iterated in reverse so that the most-significant byte is converted first.

Thus, an index pointer is set to the most significant digit (the last element) in the byte array:

var idx = bytes.Length - 1;

To capture the converted bytes, a StringBuilder is created:

var base2 = new StringBuilder(bytes.Length * 8);

The StringBuilder constructor takes the capacity for the StringBuilder. The capacity needed for the StringBuilder is calculated by taking the number of bytes to convert multiplied by eight (eight binary digits result from each byte converted).

The first byte is then converted to a binary string:

var binary = Convert.ToString(bytes[idx], 2);

At this point, it is necessary to ensure that a leading zero exists if the BigInteger is a positive value (see discussion above). If the first converted digit is not a zero, and bigint is positive, then a '0' is appended to the StringBuilder:

// Ensure leading zero exists if value is positive.
if (binary[0] != '0' && bigint.Sign == 1)
{
base2.Append('0');
}

Next, the converted byte is appended to the StringBuilder:

base2.Append(binary);

To convert the remaining bytes, a loop iterates the remainder of the byte array in reverse order:

for (idx--; idx >= 0; idx--)
{
base16.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
}

Notice that each converted byte is padded on the left with zeros ('0'), as necessary, so that the converted string is eight binary characters. This is extremely important. Without this padding, the hexadecimal value '101' would be converted to a binary value of '11'. The leading zeros ensure the conversion is '100000001'.

When all bytes are converted, the StringBuilder contains the complete binary string, which is returned by the extension method:

return base2.ToString();

BigInteger.ToOctalString

Converting a BigInteger to an octal (base 8) string is more complicated. The problem is octal digits represent three bits which is not an even multiple of the eight bits held in each element of the byte array created by BigInteger.ToByteArray(). To solve this problem, three bytes from the array are combined into chunks of 24-bits. Each 24-bit chunk evenly converts to eight octal characters.

The first 24-bit chunk requires some modulo math:

var extra = bytes.Length % 3;

This calculation determines how many bytes are "extra" when the entire byte array is split into three-byte (24-bit) chunks. The first conversion to octal (the most-significant digits) gets the "extra" bytes so that all remaining conversions will get three bytes each.

If there are no "extra" bytes, then the first chunk gets a full three bytes:

if (extra == 0)
{
extra = 3;
}

The first chunk is loaded into an integer variable called int24 that holds up to 24-bits. Each byte of the chunk is loaded. As additional bytes are loaded, the previous bits in int24 are left-shifted by 8-bits to make room:

int int24 = 0;
for (; extra != 0; extra--)
{
int24 <<= 8;
int24 += bytes[idx--];
}

Conversion of a 24-bit chunk to octal is accomplished by:

var octal = Convert.ToString(int24, 8);

Again, the first digit must be a leading zero if the BigInteger is a positive value:

// Ensure leading zero exists if value is positive.
if (octal[0] != '0' && bigint.Sign == 1)
{
base8.Append('0');
}

The first converted chunk is appended to the StringBuilder:

base8.Append(octal);

The remaining 24-bit chunks are converted in a loop:

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'));
}

Like the binary conversion, each converted octal string is left-padded with zeros so that '7' becomes '00000007'. This ensures that zeros won't be dropped from the middle of converted strings (i.e., '17' instead of '100000007').

Conversion to Base x?

Converting a BigInteger to other number bases could be far more complicated. As long as the number base is a power of two (i.e., 2, 4, 8, 16) the byte array created by BigInteger.ToByteArray() can be appropriately split into chunks of bits and converted.

However, if the number base is not a power of two, the problem becomes much more complicated and requires a good deal of looping and division. As such number base conversions are rare, I have only covered the popular computing number bases here.

BigInteger Parse Octal String?

This may not be the most efficient solution, but if performance is not a priority, you can construct the BigInteger manually:

string s = "16304103460644701340432043410021040424210140423204";
BigInteger bi = s.Aggregate(new BigInteger(), (b, c) => b * 8 + c - '0');

The above solution also works for any base not greater than 10; just replace the 8 in the above code with your required base.

Edit: For hexadecimal numbers, you should use the Parse method. Prepend with 0 if your number should be interpreted as positive even if its first character is 8F.

string s = "0F20051C5E45F4FD68F8E58905A133BCA";
BigInteger bi = BigInteger.Parse(s, NumberStyles.HexNumber);

Convert BigInteger Binary to BigInteger Number?

Unfortunately, there is nothing built-in in the .NET framework.

Fortunately, the StackOverflow community has already solved both problems:

  • BigInteger -> Binary: BigInteger to Hex/Decimal/Octal/Binary strings?
  • Binary -> BigInteger: C# Convert large binary string to decimal system

BigInteger to Hexadecimal

Use .ToString("X") or .ToString("x") depending on what case you prefer.

BigInteger to Hexadecimal

Use .ToString("X") or .ToString("x") depending on what case you prefer.

How to perform addition of 2 very large (over 50 digits) binary string values in C#

You can exploit the same scheme you used before but with BigInteger:

using System.Linq;
using System.Numerics;

...

BigInteger first = a.Aggregate(BigInteger.Zero, (s, item) => s * 2 + item - '0');
BigInteger second = b.Aggregate(BigInteger.Zero, (s, item) => s * 2 + item - '0');

StringBuilder sb = new StringBuilder();

for (BigInteger addresult = first + second; addresult > 0; addresult /= 2)
sb.Append(addresult % 2);

if (sb.Length <= 0)
sb.Append('0');

string result = string.Concat(sb.ToString().Reverse());

C#: BigInteger to string with radix specified

You can easily implement an extension method for this, e.g.

  using System.Linq;
using System.Numerics;

...

public static partial class BigIntegerExtensions {
// this have to be used for extend BigInteger
public static String ToRadixString(this BigInteger value, int radix) {
if (radix <= 1 || radix > 36)
throw new ArgumentOutOfRangeException(nameof(radix));
if (value == 0)
return "0";

bool negative = value < 0;

if (negative)
value = -value;

StringBuilder sb = new StringBuilder();

for (; value > 0; value /= radix) {
int d = (int)(value % radix);

sb.Append((char)(d < 10 ? '0' + d : 'A' - 10 + d));
}

return (negative ? "-" : "") + string.Concat(sb.ToString().Reverse());
}
}

And then use it:

 BigInteger x = 123456;

// HIID
Console.Write(x.ToRadixString(19));

Demo:

  BigInteger x = 123456;

string demo = string.Join(Environment.NewLine, Enumerable
.Range(2, 35)
.Select(radix => $"{x} (radix = {radix,2}) = {x.ToRadixString(radix)}"));

Console.Write(demo);

Outcome:

123456 (radix =  2) = 11110001001000000
123456 (radix = 3) = 20021100110
123456 (radix = 4) = 132021000
123456 (radix = 5) = 12422311
123456 (radix = 6) = 2351320
123456 (radix = 7) = 1022634
123456 (radix = 8) = 361100
123456 (radix = 9) = 207313
123456 (radix = 10) = 123456
123456 (radix = 11) = 84833
123456 (radix = 12) = 5B540
123456 (radix = 13) = 44268
123456 (radix = 14) = 32DC4
123456 (radix = 15) = 268A6
123456 (radix = 16) = 1E240
123456 (radix = 17) = 18232
123456 (radix = 18) = 1330C
123456 (radix = 19) = HIID
123456 (radix = 20) = F8CG
123456 (radix = 21) = D6JI
123456 (radix = 22) = BD1E
123456 (radix = 23) = A38F
123456 (radix = 24) = 8M80
123456 (radix = 25) = 7MD6
123456 (radix = 26) = 70G8
123456 (radix = 27) = 679C
123456 (radix = 28) = 5HD4
123456 (radix = 29) = 51N3
123456 (radix = 30) = 4H56
123456 (radix = 31) = 44EE
123456 (radix = 32) = 3OI0
123456 (radix = 33) = 3EC3
123456 (radix = 34) = 34R2
123456 (radix = 35) = 2URB
123456 (radix = 36) = 2N9C

Java: String to BigInteger to Binary

A string like "hey world" won't have any meaningful numeric representation in base 10, so I suggest to output an appropriate message instead.

Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
try {
BigInteger big = new BigInteger(input); // default constructor is base 10
System.out.println(big.toString(2));
}
catch (NumberFormatException){
System.out.println("Input was not a decimal number");
}


Related Topics



Leave a reply



Submit