makerofthings7
makerofthings7

Reputation: 61483

How do I convert an array of floats to a byte[] and back?

I have an array of Floats that need to be converted to a byte array and back to a float[]... can anyone help me do this correctly?

I'm working with the bitConverter class and found myself stuck trying to append the results.

The reason I'm doing this is so I can save runtime values into a IO Stream. The target storage is Azure Page blobs in case that matters. I don't care about what endian this is stored in, as long as it input matches the output.

static  byte[] ConvertFloatToByteArray(float[] floats)
        {
            byte[] ret = new byte[floats.Length * 4];// a single float is 4 bytes/32 bits

            for (int i = 0; i < floats.Length; i++)
            {
               // todo: stuck...I need to append the results to an offset of ret
                ret = BitConverter.GetBytes(floats[i]);

            }
            return ret;
        }


 static  float[] ConvertByteArrayToFloat(byte[] bytes)
{ //to do }

Upvotes: 58

Views: 95596

Answers (6)

Dennis
Dennis

Reputation: 1

Isn't it easier to use standard functions?

To an array of bytes: byte[] bmass = new byte[4]; bmass[0] = (byte)ifloat;

And back again: float ifloat = (float)bmass[0];

Or is there something I don't fully understand?

Upvotes: -1

oldcoder
oldcoder

Reputation: 46

You can access a float (or other) array directly as bytes with a union and avoid BitConverter altogether. More on C# unions here How to create a C/C++ union by using attributes in C#

using System.Runtime.InteropServices;

// create an array of 100 floats and set values
const int floatCount = 100;
var arrayUnion = new ArrayUnion(floatCount * sizeof(float));
for (var n = 0; n < floatCount; ++n)
    arrayUnion.floatArray[n] = n;

// write the byte array to a file
var filename = "c:/temp/testwrite.bin";
using (FileStream stream = new(filename, FileMode.Create, FileAccess.Write)) {
    stream.Write(arrayUnion.byteArray, 0, arrayUnion.byteArray.Length);
    }

// read the byte array back and write out the float values
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
    stream.Read(arrayUnion.byteArray, 0, arrayUnion.byteArray.Length);

    for (var n = 0; n < floatCount; ++n)
        Console.WriteLine(arrayUnion.floatArray[n]);
    }

[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
struct ArrayUnion(int byteCount) {
    [System.Runtime.InteropServices.FieldOffset(0)]
    public byte[] byteArray = new byte[byteCount];

    [System.Runtime.InteropServices.FieldOffset(0)]
    public float[] floatArray;
    }

Upvotes: 1

LukeH
LukeH

Reputation: 269628

If you're looking for performance then you could use Buffer.BlockCopy. Nice and simple, and probably about as fast as you'll get in managed code.

var floatArray1 = new float[] { 123.45f, 123f, 45f, 1.2f, 34.5f };

// create a byte array and copy the floats into it...
var byteArray = new byte[floatArray1.Length * 4];
Buffer.BlockCopy(floatArray1, 0, byteArray, 0, byteArray.Length);

// create a second float array and copy the bytes into it...
var floatArray2 = new float[byteArray.Length / 4];
Buffer.BlockCopy(byteArray, 0, floatArray2, 0, byteArray.Length);

// do we have the same sequence of floats that we started with?
Console.WriteLine(floatArray1.SequenceEqual(floatArray2));    // True

Upvotes: 122

Ani
Ani

Reputation: 113472

There's the BitConverter.ToSingle(byte[] value, int startIndex) method that should help out here.

Returns a single-precision floating point number converted from four bytes at a specified position in a byte array.

Your probably want something like (untested):

static float[] ConvertByteArrayToFloat(byte[] bytes)
{
    if(bytes == null)
        throw new ArgumentNullException("bytes");

   if(bytes.Length % 4 != 0)
        throw new ArgumentException
              ("bytes does not represent a sequence of floats");

    return Enumerable.Range(0, bytes.Length / 4)
                     .Select(i => BitConverter.ToSingle(bytes, i * 4))
                     .ToArray();
}

EDIT: Non-LINQ:

float[] floats = new float[bytes.Length / 4];

for (int i = 0; i < bytes.Length / 4; i++)
    floats[i] = BitConverter.ToSingle(bytes, i * 4);

return floats;

Upvotes: 7

Felice Pollano
Felice Pollano

Reputation: 33272

You are not moving the position when you copy the float[i] into the byte array, you should write something like

Array.Copy(BitConverter.GetBytes(float[i]),0,res,i*4);

instead of just:

ret = BitConverter.GetBytes(floats[i]);

the inverse function follow the same strategy.

Upvotes: 6

Lee
Lee

Reputation: 144206

static float[] ConvertByteArrayToFloat(byte[] bytes)
{
    if(bytes.Length % 4 != 0) throw new ArgumentException();

    float[] floats = new float[bytes.Length/4];
    for(int i = 0; i < floats.Length; i++)
    {
        floats[i] = BitConverter.ToSingle(bytes, i*4);
    }

    return floats;
}

Upvotes: 3

Related Questions