简体   繁体   English

将二进制转换为十进制

[英]Converting binary to decimal

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. 我有一个要求,我要从字符串“ 111111”生成一个二进制数,然后将其转换为十进制数以存储在数据库中。 Each digit (ie. 1/0) from this binary number signifies different access rights of user to different modules in the application. 该二进制数中的每个数字(即1/0)表示用户对应用程序中不同模块的不同访问权限。 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 到二进制数达到64的长度(即64乘以1s)时,我已经能够使用

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. 但是当长度增加到65时,它给出了OverFlow异常:对于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. 任何人都可以提出任何可行的解决方案,在这些解决方案中,我可以将此65长度的二进制数转换为十进制或任何其他形式以将其保存在数据库中。

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: 要将长度为65或更大的二进制数转换为十进制/任何数字,您都必须编写如下方法:

public BigInteger FromBinaryString(string binary)
{
    if (binary == null)
        throw new ArgumentNullException();
    if (!binary.All(c => c == '0' || c == '1'))
        throw new InvalidOperationException();

    BigInteger result = 0;
    foreach (var c in binary)
    {
        result <<= 1;
        result += (c - '0');
    }
    return result;
}

which uses System.Numerics.BigInteger structure to hold big numbers. 它使用System.Numerics.BigInteger结构保存大数。 Then you could explicitly convert it to decimal (or to a byte array) and store it in your database: 然后,您可以将其显式转换为decimal (或字节数组)并将其存储在数据库中:

var bigInteger = FromBinaryString("100000000000000000000000000000000000000000000000000000000000000000");
// to decimal
var dec = (decimal)bigInteger;
// to byte array
var data = bigInteger.ToByteArray();

EDIT : If you're under NET 3.5, just use decimal instead of BigInteger (also replace left-shift operator << with the multiplication operator * for decimals): 编辑 :如果您在NET 3.5下,只需使用decimal而不是BigInteger (也可以将左移运算符<<替换为乘法运算符*作为十进制):

public decimal FromBinaryString(string binary)
{
    if (binary == null)
        throw new ArgumentNullException();
    if (!binary.All(c => c == '0' || c == '1'))
        throw new InvalidOperationException();

    decimal result = 0;
    foreach (var c in binary)
    {
        result *= 2;
        result += (c - '0');
    }
    return result;
}

The normal numeric types are not going to be appropriate for more than 64 bits of data, either in SQL Server or in C#. 普通数字类型不适用于SQL Server或C#中超过64位的数据。

Your other alternatives include: 您的其他替代方法包括:

  • A uniqueidentifier (GUID) field, which would provide up to 128 bits. 唯一uniqueidentifier (GUID)字段,最多可提供128位。
  • A binary or varbinary field, which could be used for just about as many bits as you can dream up. binaryvarbinary字段,可用于您梦dream以求的位。
  • Individual bit fields for each access right. 每个访问权限的各个位字段。 SQL Server will pack them into bytes automatically. SQL Server会自动将它们打包为字节。 This would be a strong candidate if you'll be querying the data directly rather than just persisting it. 如果您将直接查询数据而不是仅持久保存数据,那么这将是一个很好的选择。
  • A string, as already mentioned, but this would be a terrible waste of space. 如前所述,字符串,但这将是对空间的巨大浪费。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM