1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| public partial class BinaryUtils { #region private methods
private static void WriteInt16(short value, byte[] targetBytes, ref int index, bool isBigEndian) { if (isBigEndian) BinaryPrimitives.WriteInt16BigEndian(targetBytes.AsSpan(index), value); else BinaryPrimitives.WriteInt16LittleEndian(targetBytes.AsSpan(index), value); index += sizeof(short); } private static void WriteUInt16(ushort value, byte[] targetBytes, ref int index, bool isBigEndian) { if (isBigEndian) BinaryPrimitives.WriteUInt16BigEndian(targetBytes.AsSpan(index), value); else BinaryPrimitives.WriteUInt16LittleEndian(targetBytes.AsSpan(index), value); index += sizeof(ushort); } private static void WriteInt32(int value, byte[] targetBytes, ref int index, bool isBigEndian) { if (isBigEndian) BinaryPrimitives.WriteInt32BigEndian(targetBytes.AsSpan(index), value); else BinaryPrimitives.WriteInt32LittleEndian(targetBytes.AsSpan(index), value); index += sizeof(int); } private static void UWriteInt32(uint value, byte[] targetBytes, ref int index, bool isBigEndian) { if (isBigEndian) BinaryPrimitives.WriteUInt32BigEndian(targetBytes.AsSpan(index), value); else BinaryPrimitives.WriteUInt32LittleEndian(targetBytes.AsSpan(index), value); index += sizeof(uint); } private static void WriteInt64(long value, byte[] targetBytes, ref int index, bool isBigEndian) { if (isBigEndian) BinaryPrimitives.WriteInt64BigEndian(targetBytes.AsSpan(index), value); else BinaryPrimitives.WriteInt64LittleEndian(targetBytes.AsSpan(index), value); index += sizeof(long); } private static void UWriteInt64(ulong value, byte[] targetBytes, ref int index, bool isBigEndian) { if (isBigEndian) BinaryPrimitives.WriteUInt64BigEndian(targetBytes.AsSpan(index), value); else BinaryPrimitives.WriteUInt64LittleEndian(targetBytes.AsSpan(index), value); index += sizeof(ulong); }
#endregion
public static void Write<T>(T obj, byte[] targetBytes, ref int index, bool isBigEndian) where T : IPackable<T>, new() { var data1 = obj.Serialize(isBigEndian); data1.CopyTo(targetBytes, index); index += data1.Length; }
public static void Write<T>(T[] obj, byte[] targetBytes, ref int index, bool isBigEndian) where T : IPackable<T>, new() { foreach (var item in obj) { var data1 = item.Serialize(isBigEndian); data1.CopyTo(targetBytes, index); index += data1.Length; } }
public static void Write(bool value, byte[] targetBytes, ref int index, bool isBigEndian) => targetBytes[index++] = value ? FlagTrue : FlagFalse;
public static void Write(byte value, byte[] targetBytes, ref int index, bool isBigEndian) => targetBytes[index++] = value; public static void Write(sbyte value, byte[] targetBytes, ref int index, bool isBigEndian) => targetBytes[index++] = (byte)value;
public static void Write(char value, byte[] targetBytes, ref int index, bool isBigEndian) => WriteInt16((short)value, targetBytes, ref index, isBigEndian);
public static void Write(short value, byte[] targetBytes, ref int index, bool isBigEndian) => WriteInt16(value, targetBytes, ref index, isBigEndian);
public static void Write(ushort value, byte[] targetBytes, ref int index, bool isBigEndian) => WriteInt16((short)value, targetBytes, ref index, isBigEndian);
public static void Write(int value, byte[] targetBytes, ref int index, bool isBigEndian) => WriteInt32(value, targetBytes, ref index, isBigEndian);
public static void Write(uint value, byte[] targetBytes, ref int index, bool isBigEndian) => WriteInt32((int)value, targetBytes, ref index, isBigEndian);
public static void Write(long value, byte[] targetBytes, ref int index, bool isBigEndian) => WriteInt64(value, targetBytes, ref index, isBigEndian);
public static void Write(ulong value, byte[] targetBytes, ref int index, bool isBigEndian) => WriteInt64((long)value, targetBytes, ref index, isBigEndian);
public static void Write(float value, byte[] targetBytes, ref int index, bool isBigEndian) { unsafe { WriteInt32(*(int*)&value, targetBytes, ref index, isBigEndian); } }
public static void Write(double value, byte[] targetBytes, ref int index, bool isBigEndian) { unsafe { WriteInt64(*(long*)&value, targetBytes, ref index, isBigEndian); } }
public static void Write(byte[] value, byte[] targetBytes,ref int index, bool isBigEndian) { int byteCount = value.Length * sizeof(byte); Buffer.BlockCopy(value, 0, targetBytes, index, byteCount); index += byteCount; } }
|