How do I turn a binary string into a float or double?
Solution 1
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
Solution 2
double d1 = 1234.5678;
string ds = DoubleToBinaryString(d1);
double d2 = BinaryStringToDouble(ds);
float f1 = 654.321f;
string fs = SingleToBinaryString(f1);
float f2 = BinaryStringToSingle(fs);
// ...
public static string DoubleToBinaryString(double d)
{
return Convert.ToString(BitConverter.DoubleToInt64Bits(d), 2);
}
public static double BinaryStringToDouble(string s)
{
return BitConverter.Int64BitsToDouble(Convert.ToInt64(s, 2));
}
public static string SingleToBinaryString(float f)
{
byte[] b = BitConverter.GetBytes(f);
int i = BitConverter.ToInt32(b, 0);
return Convert.ToString(i, 2);
}
public static float BinaryStringToSingle(string s)
{
int i = Convert.ToInt32(s, 2);
byte[] b = BitConverter.GetBytes(i);
return BitConverter.ToSingle(b, 0);
}
Solution 3
The same as in Marc's answer, you need BitConverter
again:
- http://msdn.microsoft.com/en-us/library/system.bitconverter.todouble.aspx
- http://msdn.microsoft.com/en-us/library/system.bitconverter.tosingle.aspx
Related videos on Youtube
![Tom Wright](https://i.stack.imgur.com/Dv5fj.jpg?s=256&g=1)
Tom Wright
Father, amateur zoologist, music-lover, and software developer. Aspiring blogger, occasional Tweeter, and diligent Scrobbler Head of software development at Global Pricing Innovations.
Updated on June 04, 2022Comments
-
Tom Wright about 2 years
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.)
-
Henk HoltermanYou should be more specific about 'binary string' as no such thing exists. Do you mean a string with a hex representation?
-
-
MagnatLU over 12 yearsThe guts of
BitConverter
are using pointers - here's Reflector'd body ofToDouble
:return *(((double*) &ToInt64(value, startIndex)));
-
Kerrek SB over 12 yearsThat should come with a "don't do this at home" warning, as this is flat-out undefined behaviour in C. Presumably the C# implementation is relying on some assumptions about the compiler.
-
Henk Holterman over 12 yearsI don't think the string
aabbcc
is going to turn into a double in any meaningful way. -
phoog over 12 years@KerrekSB presumably the C# implementation is relying on the C# specification.
-
Kerrek SB over 12 years@phoog: What does the C# specification say about type punning and strict aliasing rules?
-
MagnatLU over 12 years@HenkHolterman: it won't, but it's hard to tell what "binary string" meant before OPs edit.
-
phoog over 12 years@KerrekSB it specifies the binary representation of
Int64
and ofdouble
, and it specifies rules for pointer conversions.