- Home /
Converting Vector3 to Byte[]?
Hi, I'm looking for a efficient way to convert a Vector3 to a Byte[] array as I'm writing a program that'll send this Byte[] over to the server.
Currently what i have in mind is this:
Vector3 vector = Vector3.Zero;
byte[] array = new byte[3];
array[0] = vector.x;
array[1] = vector.y;
array[2] = vector.z;
Are there any other elegant ways to do this instead?
That won't work, since a Vector3 is 3 single-precision floats, which require 4 bytes each, for a total of 12.
Vector3 is marked as not being serializable for me (Unity5).
You can use an ISerializationSurrogate to serialize a Vector3.
using System.Runtime.Serialization;
using UnityEngine;
sealed class Vector3SerializationSurrogate : ISerializationSurrogate {
// $$anonymous$$ethod called to serialize a Vector3 object
public void GetObjectData(System.Object obj,
SerializationInfo info, Strea$$anonymous$$gContext context) {
Vector3 v3 = (Vector3) obj;
info.AddValue("x", v3.x);
info.AddValue("y", v3.y);
info.AddValue("z", v3.z);
//Debug.Log(v3);
}
// $$anonymous$$ethod called to deserialize a Vector3 object
public System.Object SetObjectData(System.Object obj,
SerializationInfo info, Strea$$anonymous$$gContext context,
ISurrogateSelector selector) {
Vector3 v3 = (Vector3) obj;
v3.x = (float)info.GetValue("x", typeof(float));
v3.y = (float)info.GetValue("y", typeof(float));
v3.z = (float)info.GetValue("z", typeof(float));
obj = v3;
return obj; // Formatters ignore this return value //Seems to have been fixed!
}
}
Answer by Eric5h5 · Apr 09, 2014 at 09:11 PM
Use the BitConverter class, keeping in mind that it does not take processor endianness into account.
Is there a way to find this endianness? or is it up to whoever unpacks it on the other end?
can you explain on how to do it? Bitconverter.Getbytes returns a array of bytes which i can't store it in a byte[], but i need all three axis in a single byte[], or could i convert the axis into a string, send it to the server and have the server convert it back to a float? LOL
No, do not send a string. An array of bytes is byte[]. You can just copy the 3 byte arrays into a single byte array if necessary.
Answer by Nanity · Apr 11, 2014 at 01:36 PM
Pure elegance with generic functions <3 Works like a charm!
http://answers.unity3d.com/questions/174207/not-able-to-pass-an-instance-of-a-class-over-a-net.html
Edit: Well I should give an example how your final code should look like with the link given:
Vector3 vector = Vector3.Zero;
byte[] array = InfoSender.SerializeObject(vector);
I wrote an accoriding genereic deserialize function that wasn't given in the link, very handy:
public static _T DeserializeObject<_T>(byte[] dataStream)
{
MemoryStream memStr = new MemoryStream(dataStream);
memStr.Position = 0;
BinaryFormatter bf = new BinaryFormatter();
bf.Binder = new VersionFixer();
return (_T)bf.Deserialize(memStr);
}
Answer by karl_jones · Jun 05, 2015 at 08:32 AM
Use BitConverter.GetBytes to convert each float (x,y,z) into bytes and put them into an array.
Something like this
byte[] bytes = new bytes[12]; // 4 bytes per float
Buffer.BlockCopy( BitConverter.GetBytes( ourVec.x ), 0, bytes, 0, 4 );
Buffer.BlockCopy( BitConverter.GetBytes( ourVec.y ), 0, bytes, 4, 4 );
Buffer.BlockCopy( BitConverter.GetBytes( ourVec.z ), 0, bytes, 8, 4 );
Am I missing something? I've just looked at the docs for Buffer.BlockCopy and the order of the arguments is different from yours. https://docs.microsoft.com/en-us/dotnet/api/system.buffer.blockcopy?view=netframework-4.8#System_Buffer_BlockCopy_System_Array_System_Int32_System_Array_System_Int32_System_Int32_
Should your example not read:
Buffer.BlockCopy(BitConverter.GetBytes( ourVec.y ), 0, bytes, 4, 4 );
Yes, absolutely ^^. I'm going to fix the answer. It's always a suprise that answers receive upvotes even they provide code that doesn't work
Answer by archiemdiulian · Feb 05, 2017 at 09:12 AM
Convert from Vector3 to byte[] :
byte[] buff = new byte[sizeof(float)*3];
Buffer.BlockCopy( BitConverter.GetBytes( vect.x ), 0, buff, 0*sizeof(float), sizeof(float) );
Buffer.BlockCopy( BitConverter.GetBytes( vect.y ), 0, buff, 1*sizeof(float), sizeof(float) );
Buffer.BlockCopy( BitConverter.GetBytes( vect.z ), 0, buff, 2*sizeof(float), sizeof(float) );
Convert from byte[] to Vector3:
byte[] buff = data;
Vector3 vect = Vector3.zero;
vect.x = BitConverter.ToSingle(buff,0*sizeof(float));
vect.y = BitConverter.ToSingle(buff,1*sizeof(float));
vect.z = BitConverter.ToSingle(buff,2*sizeof(float));