public static string ReadString(ByteArray array) { if (!array.Has(Sizes.IntLength)) { throw new IndexOutOfRangeException(); } var length = ReadInt(array); if (!array.Has(length)) { throw new IndexOutOfRangeException(); } var str = new StringBuilder(); for (int i = 0; i < length; i++) { str.Append(ReadChar(array)); } return(str.ToString()); }
public static unsafe void WriteInt(int value, ByteArray array) { if (!array.Has(Sizes.IntLength)) { throw new ArgumentOutOfRangeException(); } byte *ptr = (byte *)&value; for (int i = 0; i < Sizes.IntLength; i++) { array.Write(ptr[i]); } }
public static unsafe char ReadChar(ByteArray array) { if (!array.Has(Sizes.CharLength)) { throw new IndexOutOfRangeException(); } char output; byte *ptr = (byte *)&output; for (int i = 0; i < Sizes.CharLength; i++) { ptr[i] = array.Read(); } return(output); }
public static unsafe ushort ReadUShort(ByteArray array) { if (!array.Has(Sizes.UShortLength)) { throw new IndexOutOfRangeException(); } ushort output; byte * ptr = (byte *)&output; for (int i = 0; i < Sizes.UShortLength; i++) { ptr[i] = array.Read(); } return(output); }
public static unsafe double ReadDouble(ByteArray array) { if (!array.Has(Sizes.DoubleLength)) { throw new IndexOutOfRangeException(); } double output; byte * ptr = (byte *)&output; for (int i = 0; i < Sizes.DoubleLength; i++) { ptr[i] = array.Read(); } return(output); }
public static void WriteString(string value, ByteArray array) { var length = value.Length; var numBytes = 4 + length * 2; if (!array.Has(numBytes)) { throw new ArgumentOutOfRangeException(); } WriteInt(length, array); foreach (var c in value) { WriteChar(c, array); } }