C# 浮点数的二进制字符串表示返回0
当我启动这个控制台应用程序时,我得到的是C# 浮点数的二进制字符串表示返回0,c#,.net,C#,.net,当我启动这个控制台应用程序时,我得到的是0,而不是32位字符串。然而,它不会抛出错误 static void Main() { double num = 2.75; byte [] bytes = BitConverter.GetBytes(num); int toInt = BitConverter.ToInt32(bytes, 0); string bitString = Convert.ToString(to
0
,而不是32位字符串。然而,它不会抛出错误
static void Main()
{
double num = 2.75;
byte [] bytes = BitConverter.GetBytes(num);
int toInt = BitConverter.ToInt32(bytes, 0);
string bitString = Convert.ToString(toInt);
Console.WriteLine(bitString);
}
double
是64位
您看到的是前32位,它们都是零
你想要
float
(或者调用ToInt64
查看所有位)。尝试Convert.ToString((int)num,2)
@M.kazemAkhgary:那不是他想要的。@M.kazemAkhgary你打印出来的10
不能隐式地将类型“long”转换为“int”。存在显式转换(是否缺少转换?
此外,我不得不使用long
,抱怨float
将int-toInt
更改为long-toInt
@SvetanDimoff@M.kazemAkhgary为什么当我尝试对num
使用float
时,我得到类型double的Literal不能隐式转换为类型“float”;使用“F”后缀创建此类型的文字
?根据定义,2.75
adouble
,所以我不能使用float
?2.75
是double。写入2.75f
或像(float)2.75一样强制转换