Convert BigInteger Binary to BigInteger Number? - c#

Currently i am using Long integer type. I used the following to convert 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
Now the numbers i am using have exceeded 64 bit, so is started using BigInteger. I can't seem to find the equivalent of the code above.
How can i convert from a BinaryString that have over 64bits to a BigInteger number and vice versa ?
Update:
The references in the answer contains the answer i want but i am having some trouble in the conversion from Number to Binary.
I have used the following code which is available in the first reference:
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();
}
The result i got is wrong:
100001000100000000000100000110000100010000000000000000000000000000000000 ===> 2439583056328331886592
2439583056328331886592 ===> 0100001000100000000000100000110000100010000000000000000000000000000000000
If you put the resulted binary string under each other, you will notice that the conversion is correct and that the problem is that there is a leading zero from the left:
100001000100000000000100000110000100010000000000000000000000000000000000
0100001000100000000000100000110000100010000000000000000000000000000000000
I tried reading the explanation provided in the code and changing it but no luck.
Update 2:
I was able to solve it by changing the following in the code:
// 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);
}

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

There is a good reference on MSDN about BigIntegers. Can you check it?
https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx
Also there is a post to convert from binary to biginteger Conversion of a binary representation stored in a list of integers (little endian) into a Biginteger
This example is from 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

Related

Storing numbers larger than Big integer C#

I am having a really hard time finding a way to store massive prime numbers in c#. I tried everything but nothing worked out for me. For example. How can I store this number.
0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF
Do you know how or what an external library that could store that?
Thanks!!!
The size of an integer representable by BigInteger is effectively only constrained by the maximum addressable memory of the program if not the computer itself. You can parse one by using BigInteger.Parse or BigInteger.TryParse and passing NumberStyles.HexNumber (minus the "0x" part of the string).
A note about Parse, if the first digit is "8" or higher, that will result in the first bit of the number's binary representation being a 1. Signed integers interpret this as being a negative number, so your resulting number will not only be a negative number, but it will be very different than the equivalent positive number's binary representation. To avoid this, prepend a "0" to the input string.
You can "store" it by either converting it back into a string with ToString or by converting it into a byte array with ToByteArray, appropriately enough.
var input = "0FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF";
var hugeNumber = BigInteger.Parse(input, NumberStyles.HexNumber);
var hugeNumberString = hugeNumber.ToString();
var hugeNumberBytes = hugeNumber.ToByteArray();
// You can reload the byte array by simply passing it to the `BigInteger` constructor.
var hugeNumberReconstructed = new BigInteger(hugeNumberBytes);
BigInteger does it
var bytes = new List<Byte>();
string num = #"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF";
for (int i = 0; i < num.Length; i+=2) {
var b = num.Substring(i, 2);
var x = Convert.ToByte(b, 16);
bytes.Add(x);
}
var bn = new System.Numerics.BigInteger(bytes.ToArray());
debugger shows
bn {-1570566720338582667927906308486331020678313192057614971584760731901058647139518057241123644094401330702204896434749395740948521204256830813802793411121615133171266951981018749121361010262657119536344690188227760522346385933065780960593402202465448470021036386883323238183599243975089536777184470963161425724987363228043371573877576452029605409494617533656095430483815839481907153364288401182596736380740919039955960610857082518546213313320880567980770439331102549009223218208548749082963734827189358268113429507100272583973032847664061854471533479116667574516065732839447648992011110538140617869829020597826272370250406178983259310517449686885092778023735816105275313156168057770834629675841435934157964159733407369373150569643832403368714414248477830355864648343905813412169307812679570071021264327268776159178258126189105355201727008666110957901070869422442964928204432497893639569923030486663520936478372141324725647779388824026436194349030123028127183590807988699910718883036274092296195738801238932240607706512065663168315105279062647762457541689320199801400963441351542207060400661407720411212987665329161135568453084989958577002926963319659401211071077577114413531153489686602746402174695962726389204418249748806498154334309175313426956505725867430885326612261827698634725487008721538567095488663671271012738141457858408427283049274115742675742516750037999118278841924003978633759158117873692177310838874149259276441739874499926101302480541938686564170604159164760763295532542904951250793468087996706862009509879288767160917718363858884333087557922625562601699376322568867851374429539180956730361131113520484694955462251349689481650791574910892992752905760719949241290478195310550499570804467728132132006347697735259131798874824194406553202473938274024972466003838104776740077783578318451208991087791302742074840248125227009}
BigInteger, but you need to add a Zero at the begining of the hexadecimal string number, to avoid get a negative number
public Class1()
{
var cadNumber =
"0FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF";
decimal.TryParse(
cadNumber
,out decimal numberDecimal);
var numberBigInteger = BigInteger.Parse(cadNumber, NumberStyles.AllowHexSpecifier);
var numberLong = long.Parse(cadNumber, NumberStyles.AllowHexSpecifier);
}

EBCDIC to ASCII conversion, handling numeric values

I’m attempting to convert files from ECDIC to ASCII format and have run into an interesting issue. The files contain fixed length records with some fields being signed binary integers (described as B4 in the record layout), and long-precision numeric values (described as L8 in the record layout). I’ve been able to convert character data with no problem, but I’m not sure how to go about converting these numeric values. From a reference manual for the original system (an IBM 5110), the fields are described below.
B indicates the length (2, 4, or 8 bytes) of numeric data items in
fixed-point signed binary integer format that are to be converted to
BASIC internal data format. For record I/O file input, the next 2,
4, or 8 bytes in the record contain a signed binary value to be
converted by the system into internal data format and assigned to the
variable(s) specified in the READ FILE or REREAD FILE statement using
a FORM statement.
and
L indicates long-precision (8 characters) for numeric values. For
input, this entry indicates that an eight-position, long-precision
value in the record is to be assigned without conversion to a
corresponding numeric variable specified in the READ FILE or REREAD
FILE statement.
EDIT: Here's the code I'm using for the conversion
private void ConvertFile(EbcdicFile file)
{
if (file == null) return;
var filePath = Path.Combine(file.Path, file.FileName);
if (!File.Exists(filePath))
{
this.Logger.Info(string.Format("Cannot convert file {0}. It does not exist.", filePath));
return;
}
var ebcdic = Encoding.GetEncoding(37);
string convertedFilepath = Path.Combine(file.Path, file.ConvertedFileName);
byte[] fileData = File.ReadAllBytes(filePath);
if (!file.HasNumericFields)
File.WriteAllBytes(convertedFilepath, Encoding.Convert(ebcdic, Encoding.ASCII, fileData));
else
{
var convertedFileData = new List<byte>();
for (int position = 0; position < fileData.Length; position += file.RecordLength)
{
var segment = new ArraySegment<byte>(fileData, position, file.RecordLength);
file.Fields.ForEach(field =>
{
var fieldSegment = segment.Array.Skip(segment.Offset + field.Start - 1).Take(field.Length);
if (field.Type.Equals("string", StringComparison.OrdinalIgnoreCase))
{
convertedFileData.AddRange(
Encoding.Convert(ebcdic, Encoding.ASCII, fieldSegment.ToArray())
);
}
else if (field.Type.Equals("B4", StringComparison.OrdinalIgnoreCase))
{
// Not sure how to convert this field
}
else if (field.Type.Equals("L8", StringComparison.OrdinalIgnoreCase))
{
// Not sure how to convert this field
}
});
}
File.WriteAllBytes(convertedFilepath, convertedFileData.ToArray());
}
}
You must first know the fixed record size. Use FileStream.Read() to read one record worth of bytes. Then Encoding.GetString() to convert it to a string.
Then fish the fields out of the record using String.SubString(). A B4 is simply a SubString call with a length of 4, L8 with a length of 8. Further convert such a field to a number with Decimal.Parse(). You may have to divide the result, it wasn't clear what fixed-point multiplier is used. Good odds for 100.
Okay, so I've figured out how to convert both fields. B4 fields are very straightforward. They are essentially a 4-byte array which can be converted to an integer.
//The IBM 5110 were big endian machines, so reverse the array
if (BitConverter.IsLittleEndian)
Array.Reverse(by);
int value = BitConverter.ToInt32(by, 0);
The L8 fields are 8-bytes arrays that represented an IBM Double Precision Float. There are many ways this can be converted to an IEEE 754 Float. A few examples can be found at:
How To Read IBM 370 Data from a Binary File
Transform between IEEE, IBM or VAX floating point number formats and bytes expressions
Here's the version I used based on guidance from the articles.
private double IbmFloatToDouble(byte[] value)
{
if (ReferenceEquals(null, value))
throw new ArgumentNullException("value");
if (BitConverter.ToInt64(value, 0) == 0)
return 0;
int exponentBias = 64;
int ibmBase = 16;
double sign = 0.0D;
int signValue = (value[0] & 0x80) >> 7;
int exponentValue = (value[0] & 0x7f);
double fraction1 = (value[1] << 16) + (value[2] << 8) + value[3];
double fraction2 = (value[4] << 24) + (value[5] << 16) + (value[6] << 8) + value[7];
double exponent24 = 16777216.0; // 2^24
double exponent56 = 72057594037927936.0; // 2^56
double mantissa1 = fraction1 / exponent24;
double mantissa2 = fraction2 / exponent56;
double mantissa = mantissa1 + mantissa2;
double exponent = Math.Pow(ibmBase, exponentValue - exponentBias);
if (signValue == 0)
sign = 1.0;
else
sign = -1.0;
return (sign * mantissa * exponent);
}

How i convert decimal number with point to binary number in c#?

In this web i see that all answer about thins like converting decimal number to binary
its refers number without point in the number(int)...
i want to know how to convert the decimal number with point like "332.434" to binary in c#
exemple i see:
using System;
namespace _01.Decimal_to_Binary
{
class DecimalToBinary
{
static void Main(string[] args)
{
Console.Write("Decimal: ");
int decimalNumber = int.Parse(Console.ReadLine());
int remainder;
string result = string.Empty;
while (decimalNumber > 0)
{
remainder = decimalNumber % 2;
decimalNumber /= 2;
result = remainder.ToString() + result;
}
Console.WriteLine("Binary: {0}",result);
}
}
}
the exemple refer to convert from int without point
thank
Just use a BitConverter to get the bytes then loop over them converting those to strings and appending the current string of bits to the previous one.
byte[] byteArray = BitConverter.GetBytes(MyDouble);
string ByteString = System.String.Empty;
for (int i = 0; i < byteArray.Length; i++)
ByteString = Convert.ToString(byteArray[i], 2).PadLeft(8, '0');
You may have to do some tinkering to get the bits in the correct order but I assume BysteString will have the high order bits on the left. Here's the MSDN page for that ToString method http://msdn.microsoft.com/en-us/library/8s62fh68.aspx
You can't simply convert non integer number to a binary format. E.g. for 3.145926 a computer keeps a sign (+/-), a number itself but with a lead zero alway (0.3141596) and a mantissa (E-1). So you need to keep all 3 parts. Read more in wikipedia http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding

16-bit signed from ASCII hexadecimal sequence [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert numbers between hexadecimal and decimal in C#?
I'm having struggle with converting to signed int using c#
lets say I have the fallowing string:
AAFE B4FE B8FE
here we have 3 samples. each sample (signed 16 bits) is written as an ASCII hexadecimal sequence of 4 digits (2x2 digits/byte).
any suggestions?
Thank you.
If you need to specify the endian-ness of the parsed values (instead of assuming that they are in little-endian byte order), then you need to place each byte in the appropriate place within the resulting short.
Note that exceptions will be thrown in HexToByte if the string values are not well formatted.
static byte HexToByte(string value, int offset)
{
string hex = value.Substring(offset, 2);
return byte.Parse(hex, NumberStyles.HexNumber);
}
static short HexToSigned16(string value, bool isLittleEndian)
{
byte first = HexToByte(value, 0);
byte second = HexToByte(value, 2);
if (isLittleEndian)
return (short)(first | (second << 8));
else
return (short)(second | (first << 8));
}
...
string[] values = "AAFE B4FE B8FE".Split();
foreach (string value in values)
{
Console.WriteLine("{0} == {1}", value, HexToSigned16(value, true));
}
You can parse strings of numbers of any standard base using overloads in the Convert class that accept a base. In this case, you'd probably want this overload.
Then you could do something like this:
var groupings = "AAFE B4FE B8FE".Split();
var converted = groupings
.Select(grouping => Convert.ToInt16(grouping, 16))
.ToList();

Convert large binary string to decimal string

I've got a large text, containing a number as a binary value. e.g '123' would be '001100010011001000110011'. EDIT: should be 1111011
Now I want to convert it to the decimal system, but the number is too large for Int64.
So, what I want: Convert a large binary string to decimal string.
This'll do the trick:
public string BinToDec(string value)
{
// BigInteger can be found in the System.Numerics dll
BigInteger res = 0;
// I'm totally skipping error handling here
foreach(char c in value)
{
res <<= 1;
res += c == '1' ? 1 : 0;
}
return res.ToString();
}

Categories

Resources