I have a requirement where I am generating a binary number from a string say “111111” and converting it to a decimal number to store in database. Each digit (ie. 1/0) from this binary number signifies different access rights of user to different modules in the application. As and when the number of modules will increase, the number of digits in this binary number will also increase. By the time when the binary number reached to the length of 64, (ie. 64 times 1s), I was able to convert it to decimal number using
Int64 dec =Convert.ToInt64(binVal,2);
And it worked fine. But when the length increased to 65, it gave an OverFlow Exception : Value was either too large or too small for a UInt64. Can anyone suggest any possible solution where I can convert this binary number of 65 length to decimal or any other form to save it in database.
Thanks in Advance.
To convert a binary number of length 65 or more to decimal/whatever you’ll have to write a method like this:
which uses
System.Numerics.BigIntegerstructure to hold big numbers. Then you could explicitly convert it todecimal(or to a byte array) and store it in your database:EDIT: If you’re under NET 3.5, just use
decimalinstead ofBigInteger(also replace left-shift operator<<with the multiplication operator*for decimals):