How do I turn a binary string into a float or double?

前端 未结 4 1954
悲哀的现实
悲哀的现实 2021-01-19 14:21

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 appropr

4条回答
  •  旧时难觅i
    2021-01-19 15:05

    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
    

提交回复
热议问题