Created
April 1, 2020 10:17
-
-
Save openroomxyz/703221db735d53f08437a9d66a0d471b to your computer and use it in GitHub Desktop.
How do I convert an array of floats to a byte[] and back?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment