2017-05-09 21:08:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Square {
|
|
|
|
|
public static class ArrayExtensions {
|
|
|
|
|
public static T[] Subset<T>(this T[] arr, int offset, int count = -1) {
|
2017-05-11 21:03:28 +00:00
|
|
|
|
var arrEnum = arr.AsEnumerable();
|
|
|
|
|
|
|
|
|
|
if(offset > 0)
|
|
|
|
|
arrEnum = arrEnum.Skip(offset);
|
|
|
|
|
if(count > 0 && count < arr.Length)
|
2017-05-26 20:28:02 +00:00
|
|
|
|
arrEnum = arrEnum.Take(count);
|
2017-05-11 21:03:28 +00:00
|
|
|
|
|
|
|
|
|
return arrEnum.ToArray();
|
2017-05-09 21:08:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T[] Range<T>(this T[] arr, int start, int end) {
|
|
|
|
|
return arr.Subset(start, end - start);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ByteArrayExtensions {
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static string Base64Encode(this byte[] bytes)
|
|
|
|
|
=> Convert.ToBase64String(bytes);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static string ToHexString(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToString(bytes).Replace("-", "");
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static string GetString(this byte[] bytes, bool isUtf8 = true)
|
|
|
|
|
=> isUtf8 ? Encoding.UTF8.GetString(bytes)
|
|
|
|
|
: Encoding.ASCII.GetString(bytes);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
|
|
|
|
public static byte[] HostToNetworkOrder(this byte[] bytes) {
|
|
|
|
|
if(BitConverter.IsLittleEndian)
|
|
|
|
|
return bytes.Reverse().ToArray();
|
|
|
|
|
else
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] NetworkToHostOrder(this byte[] bytes) {
|
2017-05-23 21:00:51 +00:00
|
|
|
|
return bytes.HostToNetworkOrder();
|
2017-05-09 21:08:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static Single UnpackFloat(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToSingle(bytes.NetworkToHostOrder(), 0);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static Double UnpackDouble(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToDouble(bytes.NetworkToHostOrder(), 0);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static Int16 UnpackInt16(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToInt16(bytes.NetworkToHostOrder(), 0);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static UInt16 UnpackUInt16(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToUInt16(bytes.NetworkToHostOrder(), 0);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static Int32 UnpackInt32(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToInt32(bytes.NetworkToHostOrder(), 0);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static UInt32 UnpackUInt32(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToUInt32(bytes.NetworkToHostOrder(), 0);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static Int64 UnpackInt64(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToInt64(bytes.NetworkToHostOrder(), 0);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static UInt64 UnpackUInt64(this byte[] bytes)
|
|
|
|
|
=> BitConverter.ToUInt64(bytes.NetworkToHostOrder(), 0);
|
2017-05-09 21:08:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|