110,538
社区成员
发帖
与我相关
我的任务
分享
string[] list = {
"011111010000",
"010101010000",
"000110010001",
"000000001000",
"111001101111",
"110010010000"
};
foreach (string i in list)
{
double n = 0;
if (i[0] == '1')
{
n -= Convert.ToInt32(i.Substring(1, 7), 2) ^ 127;
n -= ((Convert.ToInt32(i.Substring(8, 4), 2) ^ 15) + 1) / 16.0;
}
else
{
n += Convert.ToInt32(i.Substring(1, 7), 2);
n += Convert.ToInt32(i.Substring(8, 4), 2) / 16.0;
}
Console.WriteLine(n);
}
Console.ReadKey();
static double GetDoubleFromString(string input)
{
bool negative = false;
if (input[0] == '1')
{
negative = true;
StringBuilder sb = new StringBuilder();
foreach (char c in input)
sb.Append(c == '0' ? "1" : "0");
input = Convert.ToString(Convert.ToInt32(sb.ToString(), 2) + 1, 2).PadLeft(input.Length, '0');
}
string integers = input.Substring(1, input.Length - 5);
double result = Convert.ToInt32(integers, 2);
for (int i = input.Length - 4; i < input.Length; i++)
{
if (input[i] == '1')
result += Math.Pow(2, -(input.Length - i));
}
return negative ? -result : result;
}