I am a bit confused on how to deal with little endians and big endians.
I was recently creating methods for converting things to bytes and then back like so…
Click for code
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace GameSync
{
public static class SyncSerialize
{
//No new list to avoid garbage collection, though it is unsafe to use so it is read only. Use regular getbytes for new list.
static List<Byte> getBytes = new List<Byte>(2);
static ReadOnlyCollection<Byte> getBytesReadOnly = new ReadOnlyCollection<Byte>(getBytes);
public static ReadOnlyCollection<Byte> GetBytesReadOnly(UInt16 value) { return GetBytesReadOnly((Int16)value); }
public static ReadOnlyCollection<Byte> GetBytesReadOnly(Int16 value)
{
getBytes.Clear();
getBytes.Add((byte)value);
getBytes.Add((byte)(value >> 8));
return getBytesReadOnly;
}
public static List<Byte> GetBytes(UInt16 value) { return GetBytes((Int16)value); }
public static List<Byte> GetBytes(Int16 value)
{
return new List<Byte>(GetBytesReadOnly(value));
}
public static UInt16 ToUInt16(List<Byte> bytes, int startIndex) { return (UInt16)ToInt16(bytes, startIndex); }
public static Int16 ToInt16(List<Byte> bytes, int startIndex)
{
return (Int16)((bytes[startIndex + 1] << 8) | (bytes[startIndex] << 0));
}
}
}
I cant use the c# BitConverter methods since they cause garbage collection and I plan on using this for networking, meaning it will be ran very frequently.
I later stumbled upon little endians and big endians.
To my understanding, the way I am handling things above is creating a byte list in little endian order.
I have read things like how c# handles everything in little endians and how networking is done in big endians etc…
My question is, do I need to worry about any of this? I plan on using this code to not only send packets over the network using unitys NetworkTransport.Send, but to also save the bytes in a file for game replays.
If someone on a little endians machine played my game and someone on a big endians machine played my game and used my Serialize class, will they not be able to communicate through the network? If the big endian sent a replay file that was created using my Serialize class (saved with something like File.WriteAllBytes(“MyReplay.replay”, bytesArray) and sent it to the little endians computer, will there be problems?
Am I going to have to check BitConverter.IsLittleEndian for everything that has to do with bytes and convert it?
When do I have to worry about this stuff?
I have seen this thread Handling of the bitwise/endian problem? - Unity Engine - Unity Discussions , but it is from 2006 and I don’t know what they mean by “Unity handles it”
Are they still handling it? What are they handling and how?
I am assuming my code will work on any machine as long as the bytes were created in little endians, which they are in my class, regardless to what machine the bytes were created on, and regardless to what network (internet) they use.
If anyone has answers, please let me know.
Thanks in advance =)