Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************** ApLib.Expansions ******************/
- /*********** Simply made for Convenience ***********/
- /**** <3 Mojobojo for Random Integer Conversions ***/
- /********** ie; from Int48, Int56 etc *************/
- public static class Expansions_FromBytes
- {
- public static short ToInt16(this byte[] bytes, bool isBigEndian = true)
- {
- if (isBigEndian)
- Array.Reverse(bytes);
- return BitConverter.ToInt16(bytes, 0);
- }
- public static ushort ToUInt16(this byte[] bytes, bool isBigEndian = true)
- {
- if (isBigEndian)
- Array.Reverse(bytes);
- return BitConverter.ToUInt16(bytes, 0);
- }
- public static int ToInt24(byte[] buffer, bool isBigEndian = true)
- {
- if (buffer.Length > 3 || buffer.Length < 3)
- return 0;
- if (isBigEndian)
- Array.Reverse(buffer);
- return (((buffer[2] << 0x10) | (buffer[1] << 8)) | buffer[0]);
- }
- public static uint ToUInt24(byte[] buffer, bool isBigEndian = true)
- {
- if (buffer.Length > 3 || buffer.Length < 3)
- return 0;
- if (isBigEndian)
- Array.Reverse(buffer);
- return (uint)(((buffer[2] << 0x10) | (buffer[1] << 8)) | buffer[0]);
- }
- public static int ToInt32(this byte[] bytes, bool isBigEndian = true)
- {
- if (isBigEndian)
- Array.Reverse(bytes);
- return BitConverter.ToInt32(bytes, 0);
- }
- public static uint ToUInt32(this byte[] bytes, bool isBigEndian = true)
- {
- if (isBigEndian)
- Array.Reverse(bytes);
- return BitConverter.ToUInt32(bytes, 0);
- }
- public static long ToInt40(this byte[] buffer, bool isBigEndian = true)
- {
- if (buffer.Length > 5 || buffer.Length < 5)
- return 0;
- if (isBigEndian)
- Array.Reverse(buffer);
- return (long)(((((buffer[4] << 0x20) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
- }
- public static ulong ToUInt40(this byte[] buffer, bool isBigEndian = true)
- {
- if (buffer.Length > 5 || buffer.Length < 5)
- return 0;
- if (isBigEndian)
- Array.Reverse(buffer);
- return (ulong)(((((buffer[4] << 0x20) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
- }
- public static long ToInt48(this byte[] buffer, bool isBigEndian = true)
- {
- if (buffer.Length > 6 || buffer.Length < 6)
- return 0;
- if (isBigEndian)
- Array.Reverse(buffer);
- return (long)((((((buffer[5] << 40) | (buffer[4] << 0x20)) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
- }
- public static ulong ToUInt48(this byte[] buffer, bool isBigEndian = true)
- {
- if (buffer.Length > 6 || buffer.Length < 6)
- return 0;
- if (isBigEndian)
- Array.Reverse(buffer);
- return (ulong)((((((buffer[5] << 40) | (buffer[4] << 0x20)) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
- }
- public static long ToInt56(this byte[] buffer, bool isBigEndian = true)
- {
- if (buffer.Length > 7 || buffer.Length < 7)
- return 0;
- if (isBigEndian)
- Array.Reverse(buffer);
- return (long)(((((((buffer[6] << 0x30) | (buffer[5] << 40)) | (buffer[4] << 0x20)) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
- }
- public static ulong ToUInt56(this byte[] buffer, bool isBigEndian = true)
- {
- if (buffer.Length > 7 || buffer.Length < 7)
- return 0;
- if (isBigEndian)
- Array.Reverse(buffer);
- return (ulong)(((((((buffer[6] << 0x30) | (buffer[5] << 40)) | (buffer[4] << 0x20)) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
- }
- public static long ToInt64(this byte[] bytes, bool isBigEndian = true)
- {
- if (isBigEndian)
- Array.Reverse(bytes);
- return BitConverter.ToInt64(bytes, 0);
- }
- public static ulong ToUInt64(this byte[] bytes, bool isBigEndian = true)
- {
- if (isBigEndian)
- Array.Reverse(bytes);
- return BitConverter.ToUInt64(bytes, 0);
- }
- public static float ToSingle(this byte[] bytes, bool isBigEndian = true)
- {
- if (isBigEndian)
- Array.Reverse(bytes);
- return BitConverter.ToSingle(bytes, 0);
- }
- public static double ToDouble(this byte[] bytes, bool isBigEndian = true)
- {
- if (isBigEndian)
- Array.Reverse(bytes);
- return BitConverter.ToDouble(bytes, 0);
- }
- public static string ToString(this byte[] bytes)
- {
- string str = "";
- foreach (byte num in bytes)
- str = str + string.Format("{0,0:X2}", num);
- return str;
- }
- }
- public static class Expansions_ToBytes
- {
- public static byte[] ToByteArray(this bool Bool, bool isBigEndian = true)
- {
- byte[] Bytes = BitConverter.GetBytes(Bool);
- if (isBigEndian)
- Array.Reverse(Bytes);
- return Bytes;
- }
- public static byte[] ToByteArray(this string HexString)
- {
- HexString = HexString.Replace(" ", "").Replace("-", "").Replace("0x", "");
- if ((HexString.Length % 2) != 0)
- HexString = "0" + HexString;
- byte[] buffer = new byte[HexString.Length / 2];
- try
- {
- for (int i = 0; i < buffer.Length; i++)
- buffer[i] = System.Convert.ToByte(HexString.Substring(i * 2, 2), 0x10);
- return buffer;
- }
- catch
- {
- throw new Exception("Invalid byte Input");
- }
- }
- public static byte[] ToByteArray(this double Double, bool isBigEndian = true)
- {
- byte[] bytes = BitConverter.GetBytes(Double);
- if (isBigEndian)
- Array.Reverse(bytes);
- return bytes;
- }
- public static byte[] ToByteArray(this float Float, bool isBigEndian = true)
- {
- byte[] bytes = BitConverter.GetBytes(Float);
- if (isBigEndian)
- Array.Reverse(bytes);
- return bytes;
- }
- public static byte[] ToByteArray(this byte[] BytePiece, uint startOffset, uint size)
- {
- byte[] buffer = new byte[size];
- for (int i = 0; i < size; i++)
- buffer[i] = BytePiece[(int)((IntPtr)(startOffset + i))];
- return buffer;
- }
- public static byte[] ToByteArray(this decimal Decimal, bool isBigEndian = true)
- {
- byte[] bytes = BitConverter.GetBytes(long.Parse(Decimal.ToString(CultureInfo.InvariantCulture)));
- if (isBigEndian)
- Array.Reverse(bytes);
- return bytes;
- }
- public static byte[] ToByteArray(this decimal Decimal, bool isBigEndian = true, bool fBytes = true)
- {
- byte[] bytes = BitConverter.GetBytes(int.Parse(Decimal.ToString(CultureInfo.InvariantCulture)));
- if (isBigEndian)
- Array.Reverse(bytes);
- return bytes;
- }
- public static byte[] ToByteArray(this short Int16)
- {
- byte[] buffer = new byte[2];
- buffer[1] = (byte)(Int16 & 0xff);
- buffer[0] = (byte)((Int16 >> 8) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this int Int32)
- {
- byte[] buffer = new byte[4];
- buffer[3] = (byte)(Int32 & 0xff);
- buffer[2] = (byte)((Int32 >> 8) & 0xff);
- buffer[1] = (byte)((Int32 >> 0x10) & 0xff);
- buffer[0] = (byte)((Int32 >> 0x18) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this long Int64)
- {
- byte[] buffer = new byte[8];
- buffer[7] = (byte)(Int64 & 0xff);
- buffer[6] = (byte)((Int64 >> 8) & 0xff);
- buffer[5] = (byte)((Int64 >> 0x10) & 0xff);
- buffer[4] = (byte)((Int64 >> 0x18) & 0xff);
- buffer[3] = (byte)((Int64 >> 0x20) & 0xff);
- buffer[2] = (byte)((Int64 >> 40) & 0xff);
- buffer[1] = (byte)((Int64 >> 0x30) & 0xff);
- buffer[0] = (byte)((Int64 >> 0x38) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this ushort uInt16)
- {
- byte[] buffer = new byte[2];
- buffer[1] = (byte)(uInt16 & 0xff);
- buffer[0] = (byte)((uInt16 >> 8) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this uint uInt32)
- {
- byte[] buffer = new byte[4];
- buffer[3] = (byte)(uInt32 & 0xff);
- buffer[2] = (byte)((uInt32 >> 8) & 0xff);
- buffer[1] = (byte)((uInt32 >> 0x10) & 0xff);
- buffer[0] = (byte)((uInt32 >> 0x18) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this ulong uInt64)
- {
- byte[] buffer = new byte[8];
- buffer[7] = (byte)(uInt64 & 0xff);
- buffer[6] = (byte)((uInt64 >> 8) & 0xff);
- buffer[5] = (byte)((uInt64 >> 0x10) & 0xff);
- buffer[4] = (byte)((uInt64 >> 0x18) & 0xff);
- buffer[3] = (byte)((uInt64 >> 0x20) & 0xff);
- buffer[2] = (byte)((uInt64 >> 40) & 0xff);
- buffer[1] = (byte)((uInt64 >> 0x30) & 0xff);
- buffer[0] = (byte)((uInt64 >> 0x38) & 0xff);
- return buffer;
- }
- /*
- public static byte[] ToByteArray(this int Int24)
- {
- if ((Int24 < -8388608) || (Int24 > 0x7fffff))
- throw new Exception("Invalid value");
- byte[] buffer = new byte[3];
- buffer[2] = (byte)(Int24 & 0xff);
- buffer[1] = (byte)((Int24 >> 8) & 0xff);
- buffer[0] = (byte)((Int24 >> 0x10) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this long Int40)
- {
- if ((Int40 < -549755813888L) || (Int40 > 0x7fffffffffL))
- throw new Exception("Invalid value");
- byte[] buffer = new byte[5];
- buffer[4] = (byte)(Int40 & 0xff);
- buffer[3] = (byte)((Int40 >> 8) & 0xff);
- buffer[2] = (byte)((Int40 >> 0x10) & 0xff);
- buffer[1] = (byte)((Int40 >> 0x18) & 0xff);
- buffer[0] = (byte)((Int40 >> 0x20) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this long Int48)
- {
- byte[] buffer = new byte[6];
- buffer[5] = (byte)(Int48 & 0xff);
- buffer[4] = (byte)((Int48 >> 8) & 0xff);
- buffer[3] = (byte)((Int48 >> 0x10) & 0xff);
- buffer[2] = (byte)((Int48 >> 0x18) & 0xff);
- buffer[1] = (byte)((Int48 >> 0x20) & 0xff);
- buffer[0] = (byte)((Int48 >> 40) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this long Int56)
- {
- byte[] buffer = new byte[7];
- buffer[6] = (byte)(Int56 & 0xff);
- buffer[5] = (byte)((Int56 >> 8) & 0xff);
- buffer[4] = (byte)((Int56 >> 0x10) & 0xff);
- buffer[3] = (byte)((Int56 >> 0x18) & 0xff);
- buffer[2] = (byte)((Int56 >> 0x20) & 0xff);
- buffer[1] = (byte)((Int56 >> 40) & 0xff);
- buffer[0] = (byte)((Int56 >> 0x30) & 0xff);
- return buffer;
- }
- */
- /*
- public static byte[] ToByteArray(this uint uInt24)
- {
- if (uInt24 > 0xffffff)
- {
- throw new Exception("Invalid value");
- }
- byte[] buffer = new byte[3];
- buffer[2] = (byte)(uInt24 & 0xff);
- buffer[1] = (byte)((uInt24 >> 8) & 0xff);
- buffer[0] = (byte)((uInt24 >> 0x10) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this ulong uInt40)
- {
- if (uInt40 > 0xffffffffffL)
- {
- throw new Exception("Invalid value");
- }
- byte[] buffer = new byte[5];
- buffer[4] = (byte)(uInt40 & 0xff);
- buffer[3] = (byte)((uInt40 >> 8) & 0xff);
- buffer[2] = (byte)((uInt40 >> 0x10) & 0xff);
- buffer[1] = (byte)((uInt40 >> 0x18) & 0xff);
- buffer[0] = (byte)((uInt40 >> 0x20) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this ulong uInt48)
- {
- byte[] buffer = new byte[6];
- buffer[5] = (byte)(uInt48 & 0xff);
- buffer[4] = (byte)((uInt48 >> 8) & 0xff);
- buffer[3] = (byte)((uInt48 >> 0x10) & 0xff);
- buffer[2] = (byte)((uInt48 >> 0x18) & 0xff);
- buffer[1] = (byte)((uInt48 >> 0x20) & 0xff);
- buffer[0] = (byte)((uInt48 >> 40) & 0xff);
- return buffer;
- }
- public static byte[] ToByteArray(this ulong uInt56)
- {
- byte[] buffer = new byte[7];
- buffer[6] = (byte)(uInt56 & 0xff);
- buffer[5] = (byte)((uInt56 >> 8) & 0xff);
- buffer[4] = (byte)((uInt56 >> 0x10) & 0xff);
- buffer[3] = (byte)((uInt56 >> 0x18) & 0xff);
- buffer[2] = (byte)((uInt56 >> 0x20) & 0xff);
- buffer[1] = (byte)((uInt56 >> 40) & 0xff);
- buffer[0] = (byte)((uInt56 >> 0x30) & 0xff);
- return buffer;
- }
- */
- }
- public static class Expansions_HexStrings
- {
- public static string ToHexString(this byte[] ByteArray)
- {
- return BitConverter.ToString(ByteArray).Replace("-", " ");
- }
- public static string ToAsciiString(this string HexString, bool Reverse = false, string HexSeperator = " ")
- {
- HexString.Replace(HexSeperator, "");
- return Encoding.ASCII.GetString(HexString.ToByteArray());
- }
- public static byte[] ToByteArray(this string HexString, string HexSeperator = " ", bool isBigEndian = false)
- {
- Func<int, byte> selector = null;
- try
- {
- HexString = HexString.Replace(" ", "").Replace("0x", "").Replace("-", "").Replace(HexSeperator, "");
- if (selector == null)
- selector = x => System.Convert.ToByte(HexString.Substring(x, 2), 0x10);
- byte[] Bytes = Enumerable.ToArray<byte>(Enumerable.Select<int, byte>(
- from x in Enumerable.Range(0, HexString.Length)
- where (x % 2) == 0
- select x, selector));
- if (isBigEndian)
- Array.Reverse(Bytes);
- return Bytes;
- }
- catch (Exception)
- {
- return new byte[1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement