0

I want to convert a float array to a byte array to send it via socket to a python script. (Im doing this in the Unity engine).

I tried:

float[] myArray = {0.0f, 0.0f, 0.0f};

int len = myArray.Length;
byte[] bytes = new byte[len];
int x = 0;

foreach(float f in bytes){
  byte[] t = System.BitConverter.GetBytes(f);
  for(int y = 0; y<4); y++){
    bytes[y + x] = t[y];
    x += 4;
  }
}

The output is this:

Assets\PlayerScript.cs(106,27): error CS1002: ; expected

Assets\PlayerScript.cs(106,33): error CS1002: ; expected

Assets\PlayerScript.cs(106,33): error CS1513: } expected

Im not used to work with c# and cant get it to work... I also looked at some other stackoverflow code but that didnt really help.

1
  • There is an extra ) after y<4 Commented Sep 29, 2019 at 11:22

1 Answer 1

1

Try following :

           float[] myArray = {0.0f, 0.0f, 0.0f};

           int len = myArray.Length;
           List<byte> bytes = new List<byte>();

           foreach (float f in myArray)
           {
               byte[] t = System.BitConverter.GetBytes(f);
               bytes.AddRange(t);
           }
           byte[] byteArray = bytes.ToArray();
Sign up to request clarification or add additional context in comments.

Comments

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.