[英]How to convert any binary string into a number?
Suppose we have given a string str="00110011" then how can we convert it into a number?假设我们给了一个字符串 str="00110011" 那么我们如何将它转换成一个数字呢?
Actually, I want to get a binary string as a decimal integer.实际上,我想得到一个二进制字符串作为十进制 integer。
checking your request, the convertion of the base binary to decimal, follows a example c#:检查您的请求,基本二进制到十进制的转换,遵循示例 c#:
string myBinary = "00110011";
double myDecimal = 0;
int nChars = myBinary.Length-1;
int myBase = 2;
//(00110011) = (0 × 2^7) + (0 × 2^6) + (1 × 2^5) + (1 × 2^4) + (0 × 2^3) + (0 × 2^2) + (1 × 2^1) + (1 × 2^0) = 51
foreach (char c in myBinary)
{
var myNumber = int.Parse(c.ToString());
var exponential = Math.Pow(myBase, nChars);
myDecimal += myNumber * exponential;
nChars--;
}
Console.WriteLine(myDecimal);
Best Regards最好的祝福
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.