4

I have a C# NETMF project, and I need to convert a float to a byte[] and vice versa. The problem is, NETMF doesn't have System.BitConverter like .NET, so I can't really find any way of doing it without going low level and doing it myself.

I have always programmed high-level (Java, Python, C#, etc.) and have only dabbled in C++, so I don't really know how to turn a float into a byte array.

What would some sample code of a boilerplate function for doing this look like?

1

3 Answers 3

4

The StructLayoutAttribute is supported by the .NET Micro Framework, so you could use a C++ style union to get the bytes of a float (and the other way around too):

[StructLayout(LayoutKind.Explicit)]  
public struct FloatUnion
{ 
    [FieldOffset(0)] public float Value;
    [FieldOffset(0)] public byte Byte0;
    [FieldOffset(1)] public byte Byte1;
    [FieldOffset(2)] public byte Byte2;
    [FieldOffset(3)] public byte Byte3;

    public byte[] ToByteArray()
    {
        return new[] { Byte0, Byte1, Byte2, Byte3 };
    }

    public static byte[] FloatToBytes(float value)
    {
        return new FloatUnion { Value = value }.ToByteArray();
    }

    public static float BytesToFloat(byte[] bytes)
    {
        if (bytes.Length != 4) throw new ArgumentException("You must provide four bytes.");
        return new FloatUnion { Byte0 = bytes[0], Byte1 = bytes[1], Byte2 = bytes[2], Byte3 = bytes[3] }.Value;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

With the note that this may need modification to take into account endianness.
@NPSF3000: Indeed. Fortunately this can be easily adjusted.
@AllonGuralnek FieldOffset is not present in the .NETMF (System.Runtime.InteropServices)
@LordofScripts: Yes, it seems so.
1

Unlike the integers (Int32, Byte, UInt64, etc) where each value is easily determined from the status of each bit in the integer's bytes, there is no intuitive representation of a floating-point number in binary, so the IEEE defined a specification for floating point numbers: IEEE-754. Suffice to say, it isn't simple, but following the rules in the specification will allow you to serialize a .NET System.Single and System.Double instance to a series of bytes.

The desktop .NET Framework actually cheats here. The GetBytes(Single) function actually just casts the Single instance to an Int32 then copies the raw bytes into a 4-byte array using raw pointers. However you can't take this useful shortcut because the micro framework does not support pointers - even worse: the MF doesn't include BinaryWriter either. You'll have to roll your own IEEE-754 serializer I'm afraid.

Comments

0

Use a BinaryWriter on top of a MemoryStream.

2 Comments

BinaryWriter isn't included the Micro Framework ( msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx )
Even so, the BinaryWriter serializes Single using pointers and unsafe code too - I don't think that would work even if the BinaryWriter class was copied over using the Portable Class Library feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.