Advertisement
Apathy420

[.NET]ApLib.Expansions

Oct 25th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.94 KB | None | 0 0
  1.     /*************** ApLib.Expansions ******************/
  2.     /*********** Simply made for Convenience ***********/
  3.     /**** <3 Mojobojo for Random Integer Conversions ***/
  4.     /********** ie; from Int48, Int56 etc  *************/
  5.  
  6.     public static class Expansions_FromBytes
  7.     {
  8.         public static short ToInt16(this byte[] bytes, bool isBigEndian = true)
  9.         {
  10.             if (isBigEndian)
  11.                 Array.Reverse(bytes);
  12.  
  13.             return BitConverter.ToInt16(bytes, 0);
  14.         }
  15.         public static ushort ToUInt16(this byte[] bytes, bool isBigEndian = true)
  16.         {
  17.             if (isBigEndian)
  18.                 Array.Reverse(bytes);
  19.  
  20.             return BitConverter.ToUInt16(bytes, 0);
  21.         }
  22.  
  23.         public static int ToInt24(byte[] buffer, bool isBigEndian = true)
  24.         {
  25.             if (buffer.Length > 3 || buffer.Length < 3)
  26.                 return 0;
  27.  
  28.             if (isBigEndian)
  29.                 Array.Reverse(buffer);
  30.            
  31.             return (((buffer[2] << 0x10) | (buffer[1] << 8)) | buffer[0]);
  32.         }
  33.         public static uint ToUInt24(byte[] buffer, bool isBigEndian = true)
  34.         {
  35.             if (buffer.Length > 3 || buffer.Length < 3)
  36.                 return 0;
  37.            
  38.             if (isBigEndian)
  39.                 Array.Reverse(buffer);
  40.            
  41.             return (uint)(((buffer[2] << 0x10) | (buffer[1] << 8)) | buffer[0]);
  42.         }
  43.  
  44.         public static int ToInt32(this byte[] bytes, bool isBigEndian = true)
  45.         {
  46.             if (isBigEndian)
  47.                 Array.Reverse(bytes);
  48.  
  49.             return BitConverter.ToInt32(bytes, 0);
  50.         }
  51.         public static uint ToUInt32(this byte[] bytes, bool isBigEndian = true)
  52.         {
  53.             if (isBigEndian)
  54.                 Array.Reverse(bytes);
  55.  
  56.             return BitConverter.ToUInt32(bytes, 0);
  57.         }
  58.  
  59.         public static long ToInt40(this byte[] buffer, bool isBigEndian = true)
  60.         {
  61.             if (buffer.Length > 5 || buffer.Length < 5)
  62.                 return 0;
  63.  
  64.             if (isBigEndian)
  65.                 Array.Reverse(buffer);
  66.            
  67.             return (long)(((((buffer[4] << 0x20) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
  68.         }
  69.         public static ulong ToUInt40(this byte[] buffer, bool isBigEndian = true)
  70.         {
  71.             if (buffer.Length > 5 || buffer.Length < 5)
  72.                 return 0;
  73.  
  74.             if (isBigEndian)
  75.                 Array.Reverse(buffer);
  76.            
  77.             return (ulong)(((((buffer[4] << 0x20) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
  78.         }
  79.  
  80.         public static long ToInt48(this byte[] buffer, bool isBigEndian = true)
  81.         {
  82.             if (buffer.Length > 6 || buffer.Length < 6)
  83.                 return 0;
  84.            
  85.             if (isBigEndian)
  86.                 Array.Reverse(buffer);
  87.            
  88.             return (long)((((((buffer[5] << 40) | (buffer[4] << 0x20)) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
  89.         }
  90.         public static ulong ToUInt48(this byte[] buffer, bool isBigEndian = true)
  91.         {
  92.             if (buffer.Length > 6 || buffer.Length < 6)
  93.                 return 0;
  94.            
  95.             if (isBigEndian)
  96.                 Array.Reverse(buffer);
  97.            
  98.             return (ulong)((((((buffer[5] << 40) | (buffer[4] << 0x20)) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
  99.         }
  100.  
  101.         public static long ToInt56(this byte[] buffer, bool isBigEndian = true)
  102.         {
  103.             if (buffer.Length > 7 || buffer.Length < 7)
  104.                 return 0;
  105.            
  106.             if (isBigEndian)
  107.                 Array.Reverse(buffer);
  108.            
  109.             return (long)(((((((buffer[6] << 0x30) | (buffer[5] << 40)) | (buffer[4] << 0x20)) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
  110.         }
  111.         public static ulong ToUInt56(this byte[] buffer, bool isBigEndian = true)
  112.         {
  113.             if (buffer.Length > 7 || buffer.Length < 7)
  114.                 return 0;
  115.            
  116.             if (isBigEndian)
  117.                 Array.Reverse(buffer);
  118.            
  119.             return (ulong)(((((((buffer[6] << 0x30) | (buffer[5] << 40)) | (buffer[4] << 0x20)) | (buffer[3] << 0x18)) | (buffer[2] << 0x10)) | (buffer[1] << 8)) | buffer[0]);
  120.         }
  121.  
  122.         public static long ToInt64(this byte[] bytes, bool isBigEndian = true)
  123.         {
  124.             if (isBigEndian)
  125.                 Array.Reverse(bytes);
  126.  
  127.             return BitConverter.ToInt64(bytes, 0);
  128.         }
  129.         public static ulong ToUInt64(this byte[] bytes, bool isBigEndian = true)
  130.         {
  131.             if (isBigEndian)
  132.                 Array.Reverse(bytes);
  133.  
  134.             return BitConverter.ToUInt64(bytes, 0);
  135.         }
  136.  
  137.         public static float ToSingle(this byte[] bytes, bool isBigEndian = true)
  138.         {
  139.             if (isBigEndian)
  140.                 Array.Reverse(bytes);
  141.  
  142.             return BitConverter.ToSingle(bytes, 0);
  143.         }
  144.         public static double ToDouble(this byte[] bytes, bool isBigEndian = true)
  145.         {
  146.             if (isBigEndian)
  147.                 Array.Reverse(bytes);
  148.  
  149.             return BitConverter.ToDouble(bytes, 0);
  150.         }
  151.  
  152.         public static string ToString(this byte[] bytes)
  153.         {
  154.             string str = "";
  155.  
  156.             foreach (byte num in bytes)
  157.                 str = str + string.Format("{0,0:X2}", num);
  158.  
  159.             return str;
  160.         }
  161.     }
  162.    
  163.     public static class Expansions_ToBytes
  164.     {
  165.         public static byte[] ToByteArray(this bool Bool, bool isBigEndian = true)
  166.         {
  167.             byte[] Bytes = BitConverter.GetBytes(Bool);
  168.  
  169.             if (isBigEndian)
  170.                 Array.Reverse(Bytes);
  171.  
  172.             return Bytes;
  173.         }
  174.         public static byte[] ToByteArray(this string HexString)
  175.         {
  176.             HexString = HexString.Replace(" ", "").Replace("-", "").Replace("0x", "");
  177.            
  178.             if ((HexString.Length % 2) != 0)
  179.                 HexString = "0" + HexString;
  180.  
  181.             byte[] buffer = new byte[HexString.Length / 2];
  182.  
  183.             try
  184.             {
  185.                 for (int i = 0; i < buffer.Length; i++)
  186.                     buffer[i] = System.Convert.ToByte(HexString.Substring(i * 2, 2), 0x10);
  187.                
  188.                 return buffer;
  189.             }
  190.             catch
  191.             {
  192.                 throw new Exception("Invalid byte Input");
  193.             }
  194.         }
  195.         public static byte[] ToByteArray(this double Double, bool isBigEndian = true)
  196.         {
  197.             byte[] bytes = BitConverter.GetBytes(Double);
  198.            
  199.             if (isBigEndian)
  200.                 Array.Reverse(bytes);
  201.            
  202.             return bytes;
  203.         }
  204.         public static byte[] ToByteArray(this float Float, bool isBigEndian = true)
  205.         {
  206.             byte[] bytes = BitConverter.GetBytes(Float);
  207.            
  208.             if (isBigEndian)
  209.                 Array.Reverse(bytes);
  210.            
  211.             return bytes;
  212.         }
  213.         public static byte[] ToByteArray(this byte[] BytePiece, uint startOffset, uint size)
  214.         {
  215.             byte[] buffer = new byte[size];
  216.            
  217.             for (int i = 0; i < size; i++)
  218.                 buffer[i] = BytePiece[(int)((IntPtr)(startOffset + i))];
  219.            
  220.             return buffer;
  221.         }
  222.         public static byte[] ToByteArray(this decimal Decimal, bool isBigEndian = true)
  223.         {
  224.             byte[] bytes = BitConverter.GetBytes(long.Parse(Decimal.ToString(CultureInfo.InvariantCulture)));
  225.            
  226.             if (isBigEndian)
  227.                 Array.Reverse(bytes);
  228.            
  229.             return bytes;
  230.         }
  231.         public static byte[] ToByteArray(this decimal Decimal, bool isBigEndian = true, bool fBytes = true)
  232.         {
  233.             byte[] bytes = BitConverter.GetBytes(int.Parse(Decimal.ToString(CultureInfo.InvariantCulture)));
  234.            
  235.             if (isBigEndian)
  236.                 Array.Reverse(bytes);
  237.  
  238.             return bytes;
  239.         }
  240.         public static byte[] ToByteArray(this short Int16)
  241.         {
  242.             byte[] buffer = new byte[2];
  243.             buffer[1] = (byte)(Int16 & 0xff);
  244.             buffer[0] = (byte)((Int16 >> 8) & 0xff);
  245.             return buffer;
  246.         }
  247.         public static byte[] ToByteArray(this int Int32)
  248.         {
  249.             byte[] buffer = new byte[4];
  250.             buffer[3] = (byte)(Int32 & 0xff);
  251.             buffer[2] = (byte)((Int32 >> 8) & 0xff);
  252.             buffer[1] = (byte)((Int32 >> 0x10) & 0xff);
  253.             buffer[0] = (byte)((Int32 >> 0x18) & 0xff);
  254.             return buffer;
  255.         }
  256.         public static byte[] ToByteArray(this long Int64)
  257.         {
  258.             byte[] buffer = new byte[8];
  259.             buffer[7] = (byte)(Int64 & 0xff);
  260.             buffer[6] = (byte)((Int64 >> 8) & 0xff);
  261.             buffer[5] = (byte)((Int64 >> 0x10) & 0xff);
  262.             buffer[4] = (byte)((Int64 >> 0x18) & 0xff);
  263.             buffer[3] = (byte)((Int64 >> 0x20) & 0xff);
  264.             buffer[2] = (byte)((Int64 >> 40) & 0xff);
  265.             buffer[1] = (byte)((Int64 >> 0x30) & 0xff);
  266.             buffer[0] = (byte)((Int64 >> 0x38) & 0xff);
  267.             return buffer;
  268.         }
  269.         public static byte[] ToByteArray(this ushort uInt16)
  270.         {
  271.             byte[] buffer = new byte[2];
  272.             buffer[1] = (byte)(uInt16 & 0xff);
  273.             buffer[0] = (byte)((uInt16 >> 8) & 0xff);
  274.             return buffer;
  275.         }
  276.         public static byte[] ToByteArray(this uint uInt32)
  277.         {
  278.             byte[] buffer = new byte[4];
  279.             buffer[3] = (byte)(uInt32 & 0xff);
  280.             buffer[2] = (byte)((uInt32 >> 8) & 0xff);
  281.             buffer[1] = (byte)((uInt32 >> 0x10) & 0xff);
  282.             buffer[0] = (byte)((uInt32 >> 0x18) & 0xff);
  283.             return buffer;
  284.         }
  285.         public static byte[] ToByteArray(this ulong uInt64)
  286.         {
  287.             byte[] buffer = new byte[8];
  288.             buffer[7] = (byte)(uInt64 & 0xff);
  289.             buffer[6] = (byte)((uInt64 >> 8) & 0xff);
  290.             buffer[5] = (byte)((uInt64 >> 0x10) & 0xff);
  291.             buffer[4] = (byte)((uInt64 >> 0x18) & 0xff);
  292.             buffer[3] = (byte)((uInt64 >> 0x20) & 0xff);
  293.             buffer[2] = (byte)((uInt64 >> 40) & 0xff);
  294.             buffer[1] = (byte)((uInt64 >> 0x30) & 0xff);
  295.             buffer[0] = (byte)((uInt64 >> 0x38) & 0xff);
  296.             return buffer;
  297.         }
  298.  
  299.         /*
  300.         public static byte[] ToByteArray(this int Int24)
  301.         {
  302.             if ((Int24 < -8388608) || (Int24 > 0x7fffff))
  303.                 throw new Exception("Invalid value");
  304.  
  305.             byte[] buffer = new byte[3];
  306.             buffer[2] = (byte)(Int24 & 0xff);
  307.             buffer[1] = (byte)((Int24 >> 8) & 0xff);
  308.             buffer[0] = (byte)((Int24 >> 0x10) & 0xff);
  309.             return buffer;
  310.         }
  311.  
  312.         public static byte[] ToByteArray(this long Int40)
  313.         {
  314.             if ((Int40 < -549755813888L) || (Int40 > 0x7fffffffffL))
  315.                 throw new Exception("Invalid value");
  316.            
  317.             byte[] buffer = new byte[5];
  318.             buffer[4] = (byte)(Int40 & 0xff);
  319.             buffer[3] = (byte)((Int40 >> 8) & 0xff);
  320.             buffer[2] = (byte)((Int40 >> 0x10) & 0xff);
  321.             buffer[1] = (byte)((Int40 >> 0x18) & 0xff);
  322.             buffer[0] = (byte)((Int40 >> 0x20) & 0xff);
  323.             return buffer;
  324.         }
  325.  
  326.         public static byte[] ToByteArray(this long Int48)
  327.         {
  328.             byte[] buffer = new byte[6];
  329.             buffer[5] = (byte)(Int48 & 0xff);
  330.             buffer[4] = (byte)((Int48 >> 8) & 0xff);
  331.             buffer[3] = (byte)((Int48 >> 0x10) & 0xff);
  332.             buffer[2] = (byte)((Int48 >> 0x18) & 0xff);
  333.             buffer[1] = (byte)((Int48 >> 0x20) & 0xff);
  334.             buffer[0] = (byte)((Int48 >> 40) & 0xff);
  335.             return buffer;
  336.         }
  337.  
  338.         public static byte[] ToByteArray(this long Int56)
  339.         {
  340.             byte[] buffer = new byte[7];
  341.             buffer[6] = (byte)(Int56 & 0xff);
  342.             buffer[5] = (byte)((Int56 >> 8) & 0xff);
  343.             buffer[4] = (byte)((Int56 >> 0x10) & 0xff);
  344.             buffer[3] = (byte)((Int56 >> 0x18) & 0xff);
  345.             buffer[2] = (byte)((Int56 >> 0x20) & 0xff);
  346.             buffer[1] = (byte)((Int56 >> 40) & 0xff);
  347.             buffer[0] = (byte)((Int56 >> 0x30) & 0xff);
  348.             return buffer;
  349.         }
  350.         */
  351.         /*
  352.                  public static byte[] ToByteArray(this uint uInt24)
  353.         {
  354.             if (uInt24 > 0xffffff)
  355.             {
  356.                 throw new Exception("Invalid value");
  357.             }
  358.             byte[] buffer = new byte[3];
  359.             buffer[2] = (byte)(uInt24 & 0xff);
  360.             buffer[1] = (byte)((uInt24 >> 8) & 0xff);
  361.             buffer[0] = (byte)((uInt24 >> 0x10) & 0xff);
  362.             return buffer;
  363.         }
  364.  
  365.         public static byte[] ToByteArray(this ulong uInt40)
  366.         {
  367.             if (uInt40 > 0xffffffffffL)
  368.             {
  369.                 throw new Exception("Invalid value");
  370.             }
  371.             byte[] buffer = new byte[5];
  372.             buffer[4] = (byte)(uInt40 & 0xff);
  373.             buffer[3] = (byte)((uInt40 >> 8) & 0xff);
  374.             buffer[2] = (byte)((uInt40 >> 0x10) & 0xff);
  375.             buffer[1] = (byte)((uInt40 >> 0x18) & 0xff);
  376.             buffer[0] = (byte)((uInt40 >> 0x20) & 0xff);
  377.             return buffer;
  378.         }
  379.  
  380.         public static byte[] ToByteArray(this ulong uInt48)
  381.         {
  382.             byte[] buffer = new byte[6];
  383.             buffer[5] = (byte)(uInt48 & 0xff);
  384.             buffer[4] = (byte)((uInt48 >> 8) & 0xff);
  385.             buffer[3] = (byte)((uInt48 >> 0x10) & 0xff);
  386.             buffer[2] = (byte)((uInt48 >> 0x18) & 0xff);
  387.             buffer[1] = (byte)((uInt48 >> 0x20) & 0xff);
  388.             buffer[0] = (byte)((uInt48 >> 40) & 0xff);
  389.             return buffer;
  390.         }
  391.  
  392.         public static byte[] ToByteArray(this ulong uInt56)
  393.         {
  394.             byte[] buffer = new byte[7];
  395.             buffer[6] = (byte)(uInt56 & 0xff);
  396.             buffer[5] = (byte)((uInt56 >> 8) & 0xff);
  397.             buffer[4] = (byte)((uInt56 >> 0x10) & 0xff);
  398.             buffer[3] = (byte)((uInt56 >> 0x18) & 0xff);
  399.             buffer[2] = (byte)((uInt56 >> 0x20) & 0xff);
  400.             buffer[1] = (byte)((uInt56 >> 40) & 0xff);
  401.             buffer[0] = (byte)((uInt56 >> 0x30) & 0xff);
  402.             return buffer;
  403.         }
  404. */
  405.  
  406.     }
  407.  
  408.     public static class Expansions_HexStrings
  409.     {
  410.         public static string ToHexString(this byte[] ByteArray)
  411.         {
  412.             return BitConverter.ToString(ByteArray).Replace("-", " ");
  413.         }
  414.         public static string ToAsciiString(this string HexString, bool Reverse = false, string HexSeperator = " ")
  415.         {
  416.             HexString.Replace(HexSeperator, "");
  417.             return Encoding.ASCII.GetString(HexString.ToByteArray());
  418.         }
  419.         public static byte[] ToByteArray(this string HexString, string HexSeperator = " ", bool isBigEndian = false)
  420.         {
  421.             Func<int, byte> selector = null;
  422.             try
  423.             {
  424.                 HexString = HexString.Replace(" ", "").Replace("0x", "").Replace("-", "").Replace(HexSeperator, "");
  425.                
  426.                 if (selector == null)
  427.                     selector = x => System.Convert.ToByte(HexString.Substring(x, 2), 0x10);
  428.                
  429.                 byte[] Bytes = Enumerable.ToArray<byte>(Enumerable.Select<int, byte>(
  430.                     from x in Enumerable.Range(0, HexString.Length)
  431.                     where (x % 2) == 0
  432.                     select x, selector));
  433.                    
  434.                 if (isBigEndian)
  435.                     Array.Reverse(Bytes);
  436.  
  437.                 return Bytes;
  438.             }
  439.             catch (Exception)
  440.             {
  441.                 return new byte[1];
  442.             }
  443.         }
  444.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement