如何打开一个二进制字符串转换成float或double? [英] How do I turn a binary string into a float or double?

查看:195
本文介绍了如何打开一个二进制字符串转换成float或double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题,比尔蜥蜴问如何显示一个浮动或双的二进制重新presentation

我想知道的是,给予适当长度的二进制字符串,我怎么能执行相反操作(C#)?换句话说,我怎么把一个二进制字符串转换成float或double?

作为一个方面说明,还有这会不会导致一个有效的float或double任何位串?


编辑:通过二进制字符串我的意思是0和1的字符串

所以,我的投入将是这样一个字符串:

  01010101010101010101010101010101

和我的输出应为一个浮点数。 (或者,如果有64比特串中,一个双)


解决方案

 字符串BSTR =01010101010101010101010101010101;
长V = 0;
对(INT I = bstr.Length - 1; I> = 0;我 - )ν=(V&所述;&所述; 1)+(BSTR [I] - '0');
双D = BitConverter.ToDouble(BitConverter.GetBytes(v)中,0);
// D = 1.41466386031414E-314

In this question, Bill The Lizard asks how to display the binary representation of a float or double.

What I'd like to know is, given a binary string of the appropriate length, how could I perform the reverse operation (in C#)? In other words, how do I turn a binary string into a float or double?

As a side note, are there any bit strings which would not result in a valid float or double?


EDIT: By binary string I mean a string of 0s and 1s.

So, my input will be a string like this:

01010101010101010101010101010101

and my output should be a floating point number. (Or, if there were 64 bits in the string, a double.)

解决方案

string bstr = "01010101010101010101010101010101";
long v = 0;
for (int i = bstr.Length - 1; i >= 0; i--) v = (v << 1) + (bstr[i] - '0');
double d = BitConverter.ToDouble(BitConverter.GetBytes(v), 0);
// d = 1.41466386031414E-314

这篇关于如何打开一个二进制字符串转换成float或double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆