using System; using System.Text; namespace Assets.GAMA.net.Message { public unsafe struct FixedByteString { private const int MaxWordLength = 256; private const int MaxWordBytes = MaxWordLength * sizeof(char); private fixed byte _word[MaxWordLength]; public int _currentWordSize; public void SetString(string str) { if (str.Length > MaxWordLength) throw new ArgumentException($"str has a byte length of {str.Length} but max declared size for any given word is {MaxWordLength}"); fixed (byte* wordPtr = _word) { fixed (char* strPtr = str) { _currentWordSize = Encoding.UTF8.GetBytes(strPtr, str.Length, wordPtr, MaxWordBytes); } } } public string GetString() { if (_currentWordSize == 0) return string.Empty; fixed (byte* wordPtr = _word) { return Encoding.UTF8.GetString(wordPtr, _currentWordSize); } } } }