Do you know how many bytes the basic types use?

Which ones do you know?

Here’s a link for anyone interested:
https://condor.depaul.edu/sjost/nwdp/notes/cs1/CSDatatypes.htm

1 Like

While I think most people will eventually memorize them, it’s still error prone to type in values. One can use sizeof() as an alternative. examples below:

        private byte[] SerializeVector(Vector3 vector)
        {
            byte[] vectorBytes = new byte[3 * sizeof(float)];
            int vectorBytesIndex = 0;
            BitConverter.GetBytes(vector.x).CopyTo(vectorBytes, vectorBytesIndex);
            vectorBytesIndex += sizeof(float);
            BitConverter.GetBytes(vector.y).CopyTo(vectorBytes, vectorBytesIndex);
            vectorBytesIndex += sizeof(float);
            BitConverter.GetBytes(vector.z).CopyTo(vectorBytes, vectorBytesIndex);
            return vectorBytes;
        }

        private Vector3 DeserializeVector(byte[] buffer)
        {
            Vector3 result = new Vector3();
            int vectorBytesIndex = 0;
            result.x = BitConverter.ToSingle(buffer, vectorBytesIndex);
            vectorBytesIndex += sizeof(float);
            result.y = BitConverter.ToSingle(buffer, vectorBytesIndex);
            vectorBytesIndex += sizeof(float);
            result.z = BitConverter.ToSingle(buffer, vectorBytesIndex);
            return result;
        }

Once you switch to the Json saving system, these values will be meaningless, as data is quite literally stored in text format. (But it’s safe from injection hacking, so totally worth it).

1 Like

Privacy & Terms