- Home /
Convert Vector3 array (vector3[]) to byte array (byte[])
I know its very bad question to ask but whatever I'm stuck in there so please help me to get out of it. If you guys have any link or simple solution for this please help me .
P.S Once again - I want to convert array of vector 3 into byte array not vector3 in byte/bytearray.
Thanks.
You want to fit an entire vector3 inside a byte? Did you realize that you are trying to fix 96 bits of information into 8 bits? You will only get 2 bits per vector element (well, one of them can be 3 bits if you want). That's only going to let you encode 256 possible vectors. You might as well use an indexing system, where each byte is an index value into an array of vector3s. But still, only 256 possibly index values means only 256 possible vector values.
Answer by SamuelRoos · Nov 21, 2018 at 04:32 PM
What I did when I had to serialize values (like Vector3) over network, was using serialization with MemoryStream (which you can convert into a byte array).
I had three classes, one for arrays, one for ushort
and one for the actual Vector3
.
The array class requires a type T input which is the type you want to convert (in this case it's Vector3). It also takes an Action, which is either a serialize- or deserialize function from the Vector3Proxy class.
The UShort class is just for converting the array length; I was using ushort
instead of ints since the type contains less data to send.
This all relies on a system type called MemoryStream. Values from types are written in there, and can be afterwards converted into a byte array using ToArray()
.
(If you want to access the MemoryStream or the Stream class, you need to add the using System.IO
tag)
An example of doing the conversion:
If you want to convert a Vector3 array to a byte array, this is how you do it with these three classes:
using (MemoryStream memoryStream = new MemoryStream())
{
ArrayProxy<Vector3>.Serialize(
memoryStream,
yourVector3Variable,
Vector3Proxy.Serialize);
//Here is the result
bytes[] conversionArray = memoryStream.ToArray();
}
This converts the Vector3 array into a byte array. However, if you want to serialize it back from the byte array to a Vector3 array, it's done like this:
byte[] conversionArray = yourConvertedByteArray;
using (MemoryStream memoryStream = new MemoryStream(conversionArray))
{
//Here is the result
Vector3[] data = ArrayProxy<Vector3>.Deserialize(memoryStream, Vector3Proxy.Deserialize);
}
The Proxy Classes:
The array proxy class of a type T:
public static class ArrayProxy<T>
{
public static void Serialize(Stream bytes, T[] instance, Action<Stream, T> serialization)
{
UShortProxy.Serialize(bytes, (ushort)instance.Length);
foreach (T arg in instance)
{
serialization(bytes, arg);
}
}
public static T[] Deserialize(Stream bytes, ArrayProxy<T>.Deserializer<T> serialization)
{
ushort num = UShortProxy.Deserialize(bytes);
T[] array = new T[(int)num];
for (int i = 0; i < (int)num; i++)
{
array[i] = serialization(bytes);
}
return array;
}
public delegate void Serializer<U>(Stream stream, U instance);
public delegate U Deserializer<U>(Stream stream);
}
The UShort proxy class:
public static class UShortProxy
{
public static void Serialize(Stream bytes, ushort instance)
{
byte[] bytes2 = BitConverter.GetBytes(instance);
bytes.Write(bytes2, 0, bytes2.Length);
}
public static ushort Deserialize(Stream bytes)
{
byte[] array = new byte[2];
bytes.Read(array, 0, 2);
return BitConverter.ToUInt16(array, 0);
}
}
The Vector3 Proxy class:
public static class Vector3Proxy
{
public static void Serialize(Stream bytes, Vector3 instance)
{
bytes.Write(BitConverter.GetBytes(instance.x), 0, 4);
bytes.Write(BitConverter.GetBytes(instance.y), 0, 4);
bytes.Write(BitConverter.GetBytes(instance.z), 0, 4);
}
public static Vector3 Deserialize(Stream bytes)
{
byte[] array = new byte[12];
bytes.Read(array, 0, 12);
return new Vector3(BitConverter.ToSingle(array, 0), BitConverter.ToSingle(array, 4),
BitConverter.ToSingle(array, 8));
}
}
Make sure to comment if there any errors or something you'd like to know!
It may be late to comment now, but the data is surely converted to bytes I just cant make the byte[] saved data to deserialize and fill in the vector[]. When I try that the return values are zero, the vector[] which was earlier populated is then empty. I don't know what the chances are for anyone to respond after few years have passed, but I will take my chances as this is very important for me and my project. Best regards!
Your answer
Follow this Question
Related Questions
Converting Vector3[] to byte[] 0 Answers
Converting Vector3 to Byte[]? 4 Answers
Object faces -180 degrees away 1 Answer
Vector3.Lerp doesn't work on build 0 Answers
Whats the best way to get the Distance between two Vector3? 2 Answers