I'm writing something that convert data to byte[], transferring through internet, then convert back to what they were for a Unity game project.
I use BitConverter to convert int, float, etc., as the following example shows:
float aFloat = 312321f;
var bytes = BitConverter.GetBytes(aFloat);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
// sending through the internet
byte[] bytes = GetByteArrayFromTheInternet();
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
float aFloat = BitConverter.ToSingle(bytes, 0);
I do the endianness check before and after sending the data to make sure they're the same. Do I need to do this for string?
string aString = "testing";
var bytes = System.Text.Encoding.Unicode.GetBytes(aString);
// if (BitConverter.IsLittleEndian) Array.Reverse(bytes); // Do I need this line?
// sending through the internet
byte[] bytes = GetByteArrayFromTheInternet();
// if (BitConverter.IsLittleEndian) Array.Reverse(bytes); // Do I need this too?
string aString = System.Text.Encoding.Unicode.GetString(bytes);
Thanks in advance!