Convert int bits to float bits

3

I'm in the process of creating a buffer that will read/write in a banner in which I can completely eradicate the problems that comes with TCP-Segmentation. The only problem I've ran into is the float variable, everything else works fine, besides for the float. I can't find any information on how to convert int32 bits into a float.

When converting a float to int bits, the following method is used (Ripped straight out of java's source code, and converted)

private int floatToIntBits(float value)
{
    int result = BitConverter.ToInt32(BitConverter.GetBytes(value), 0);
    if (((result & 0x7F800000) == 0x7F800000) && (result & 0x80000000) != 0)
        result = 0x7fc00000;
    return result;
}

However, now I need to do the opposite, unfortunately, there isn't any functions in the BitConverter class that works with float.

I can';t find much information in the JavaDocs either, not any that I can personally make use of, You can find info here.

c#
bit-manipulation
asked on Stack Overflow Dec 1, 2014 by Hobbyist • edited Dec 1, 2014 by abatishchev

6 Answers

9

Vexingly, if you were using double and long, there is BitConverter.DoubleToInt64Bits and BitConverter.Int64BitsToDouble. I have genuinely no idea why there aren't Single / Int32 equivalents, as it forces you to create a pointless byte[] on the heap (it doesn't even let you pass in a pre-existing buffer).

If you are happy to use unsafe code, you can actually do it all in a simply data thunk, without any method calls or arrays:

public static unsafe int SingleToInt32Bits(float value) {
    return *(int*)(&value);
}
public static unsafe float Int32BitsToSingle(int value) {
    return *(float*)(&value);
}
answered on Stack Overflow Dec 1, 2014 by Marc Gravell
4

Use the BitConverter.ToSingle method:

int i = ...;
float f = BitConverter.ToSingle(BitConverter.GetBytes(i), 0);
answered on Stack Overflow Dec 1, 2014 by Dmitry
2

The keyword float is an alias for the data type System.Single.

You can use the BitConverter.ToSingle to convert four bytes into a float.

answered on Stack Overflow Dec 1, 2014 by Guffa
2

BitConverter creates some overhead and unnecessary buffer. This solution is almost as fast as unsafe conversion:

[StructLayout(LayoutKind.Explicit)]
struct FloatToInt 
{
    [FieldOffset(0)]private float f;
    [FieldOffset(0)]private int i;
    private static FloatToInt inst = new FloatToInt();
    public static int Convert(float value)
    {
        inst.f = value;
        return t.i;
    }
}
answered on Stack Overflow Dec 10, 2019 by Aberro • edited Dec 10, 2019 by Aberro
1

using the System.Runtime.CompilerServices.Unsafe nuget package

Unsafe.As<float, int>(ref value);

Will convert a float to an int

and

Unsafe.As<int, float>(ref value);

will convert an int to a float

answered on Stack Overflow Dec 19, 2018 by Yair Halberstadt
0

Targeting .NET Core we are now finally able to simply use BitConverter.SingleToInt32Bits() and BitConverter.Int32BitsToSingle()!

answered on Stack Overflow Sep 21, 2018 by D.R.

User contributions licensed under CC BY-SA 3.0