Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 33.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text;
  6.  
  7. namespace KiwiIO
  8. {
  9.     //feel free to browse around, check everything out :D big ass code.
  10.     public class FileIO
  11.     {
  12.         #region Fields
  13.  
  14.         private String fileName;
  15.         private FileStream handle;
  16.         private Stream stream;
  17.  
  18.         public Endian endian
  19.         {
  20.             set
  21.             {
  22.                 this.endian = value;
  23.             }
  24.             get
  25.             {
  26.                 return this.endian;
  27.             }
  28.         }
  29.         public Int64 Position
  30.         {
  31.             set
  32.             {
  33.                 try
  34.                 {
  35.                     this.stream.Position = value;
  36.                 }
  37.                 catch (Exception ex)
  38.                 {
  39.                     throw ex;
  40.                 }
  41.             }
  42.             get
  43.             {
  44.                 return this.stream.Position;
  45.             }
  46.         }
  47.  
  48.         public Int64 Length
  49.         {
  50.             get
  51.             {
  52.                 return this.stream.Position;
  53.             }
  54.         }
  55.  
  56.         public Int32 ReadTimeout
  57.         {
  58.             set
  59.             {
  60.                 try
  61.                 {
  62.                     this.stream.ReadTimeout = value;
  63.                 }
  64.  
  65.                 catch (Exception ex)
  66.                 {
  67.                     throw ex;
  68.                 }
  69.             }
  70.             get
  71.             {
  72.                 return this.stream.ReadTimeout;
  73.             }
  74.         }
  75.  
  76.         public Int32 WriteTimeout
  77.         {
  78.             set
  79.             {
  80.                 try
  81.                 {
  82.                     this.stream.WriteTimeout = value;
  83.                 }
  84.  
  85.                 catch (Exception ex)
  86.                 {
  87.                     throw ex;
  88.                 }
  89.             }
  90.             get
  91.             {
  92.                 return this.stream.WriteTimeout;
  93.             }
  94.         }
  95.  
  96.         #endregion
  97.  
  98.         #region Instansiation
  99.  
  100.         public FileIO()
  101.         {
  102.             this.stream = null;
  103.             this.handle = null;
  104.             this.endian = Endian.Little;
  105.             this.fileName = null;
  106.         }
  107.  
  108.         public FileIO(FileStream handle, Endian endian)
  109.         {
  110.             this.fileName = handle.Name;
  111.             this.handle = handle;
  112.             this.stream = handle;
  113.             this.endian = endian;
  114.         }
  115.  
  116.         public FileIO(Stream stream, Endian endian)
  117.         {
  118.             this.handle = (FileStream)stream;
  119.             this.fileName = this.handle.Name;
  120.             this.stream = stream;
  121.             this.endian = endian;
  122.         }
  123.  
  124.         public FileIO(string fileName, Endian endian)
  125.         {
  126.             this.fileName = fileName;
  127.             this.handle = new FileStream(this.fileName, FileMode.Open, FileAccess.ReadWrite);
  128.             this.stream = this.handle;
  129.             this.endian = endian;
  130.         }
  131.  
  132.         #endregion
  133.  
  134.         #region Misc Methods
  135.  
  136.         public Int64 FindASCIIString(String str)
  137.         {
  138.             try
  139.             {
  140.                 byte[] bytes = ASCIIEncoding.ASCII.GetBytes(str);
  141.                 long address = FindBytes(bytes);
  142.                 return address;
  143.             }
  144.  
  145.             catch (Exception ex)
  146.             {
  147.                 throw ex;
  148.             }
  149.         }
  150.  
  151.         public Int64 FindUnicodeString(String str)
  152.         {
  153.             try
  154.             {
  155.                 char[] strc = str.ToCharArray();
  156.                 char[] tempStrc = new char[(strc.Length * 2)];
  157.                 for (int x = 0; x < tempStrc.Length; x++)
  158.                     if (x != tempStrc.Length - 1)
  159.                         tempStrc[x] = '\0';
  160.  
  161.                 for (int x = 0; x < tempStrc.Length; x += 2)
  162.                 {
  163.                     if (x < tempStrc.Length - 1)
  164.                         tempStrc[x] = strc[x / 2];
  165.                 }
  166.                 str = new String(tempStrc);
  167.  
  168.                 byte[] bytes = ASCIIEncoding.ASCII.GetBytes(str);
  169.                 long address = FindBytes(bytes);
  170.                 return address;
  171.             }
  172.  
  173.             catch (Exception ex)
  174.             {
  175.                 throw ex;
  176.             }
  177.         }
  178.         public Int64 FindBytes(Byte[] value)
  179.         {
  180.             try
  181.             {
  182.                 Byte[] FBytes = File.ReadAllBytes(this.fileName);
  183.                 Int64 i, j;
  184.                 Int64 bufferlen = FBytes.Length;
  185.                 Int64 valuelen = value.Length;
  186.  
  187.                 for (i = 0; i < bufferlen; i++)
  188.                 {
  189.                     for (j = 0; j < valuelen; j++)
  190.                     {
  191.                         if (FBytes[i + j] != value[j])
  192.                             break;
  193.                         else if ((j + 1) == valuelen)
  194.                             return i;
  195.                     }
  196.                 }
  197.             }
  198.             catch (Exception ex)
  199.             {
  200.                 throw ex;
  201.             }
  202.             return -1;
  203.         }
  204.  
  205.         public void SetStream(Stream stream)
  206.         {
  207.             this.stream = stream;
  208.         }
  209.  
  210.         private void SwapByteSex(byte[] bytes)
  211.         {
  212.             try
  213.             {
  214.                 Array.Reverse(bytes);
  215.             }
  216.             catch (Exception ex)
  217.             {
  218.                 throw ex;
  219.             }
  220.         }
  221.  
  222.         public void Seek(Int64 offset)
  223.         {
  224.             this.Seek(offset, SeekOrigin.Current);
  225.         }
  226.  
  227.         public void Seek(Int64 offset, SeekOrigin origin)
  228.         {
  229.             try
  230.             {
  231.                 this.stream.Seek(offset, origin);
  232.             }
  233.  
  234.             catch (Exception ex)
  235.             {
  236.                 throw ex;
  237.             }
  238.         }
  239.  
  240.         public void Close()
  241.         {
  242.             this.stream.Close();
  243.         }
  244.  
  245.         public void Dispose()
  246.         {
  247.             this.stream.Dispose();
  248.         }
  249.  
  250.         public void Flush()
  251.         {
  252.             this.stream.Flush();
  253.         }
  254.  
  255.         public void SetLength(long length)
  256.         {
  257.             try
  258.             {
  259.                 this.stream.SetLength(length);
  260.             }
  261.  
  262.             catch (Exception x)
  263.             {
  264.                 throw x;
  265.             }
  266.         }
  267.  
  268.         #endregion
  269.  
  270.         #region Reader
  271.  
  272.         public bool ReadBoolean()
  273.         {
  274.             try
  275.             {
  276.                 return Conversions.BytesToBoolean(this.ReadByte());
  277.             }
  278.             catch (Exception ex)
  279.             {
  280.                 throw ex;
  281.             }
  282.         }
  283.  
  284.         public byte ReadByte()
  285.         {
  286.             try
  287.             {
  288.                 return this.ReadBytes(1)[0];
  289.             }
  290.             catch (Exception ex)
  291.             {
  292.                 throw ex;
  293.             }
  294.         }
  295.  
  296.         public byte[] ReadBytes(int count)
  297.         {
  298.             try
  299.             {
  300.                 byte[] bytes = new byte[count];
  301.                 this.stream.Read(bytes, 0, count);
  302.                 if (this.endian == Endian.Big)
  303.                     SwapByteSex(bytes);
  304.                 return bytes;
  305.             }
  306.             catch (Exception ex)
  307.             {
  308.                 throw ex;
  309.             }
  310.         }
  311.  
  312.         public byte[] ReadBytes(int count, Endian _endian)
  313.         {
  314.             try
  315.             {
  316.                 byte[] bytes = new byte[count];
  317.                 this.stream.Read(bytes, 0, count);
  318.                 if (_endian == Endian.Big)
  319.                     SwapByteSex(bytes);
  320.                 return bytes;
  321.             }
  322.             catch (Exception ex)
  323.             {
  324.                 throw ex;
  325.             }
  326.         }
  327.  
  328.         public double ReadDouble()
  329.         {
  330.             try
  331.             {
  332.                 byte[] bytes = new byte[8];
  333.                 this.stream.Read(bytes, 0, 8);
  334.                 return Conversions.BytesToDouble(bytes);
  335.             }
  336.             catch (Exception ex)
  337.             {
  338.                 throw ex;
  339.             }
  340.         }
  341.  
  342.         public short ReadInt16()
  343.         {
  344.             try
  345.             {
  346.                 byte[] bytes = new byte[2];
  347.                 this.stream.Read(bytes, 0, 2);
  348.                 return Conversions.BytesToInt16(bytes);
  349.             }
  350.             catch (Exception ex)
  351.             {
  352.                 throw ex;
  353.             }
  354.         }
  355.  
  356.         public int ReadInt24()
  357.         {
  358.             try
  359.             {
  360.                 byte[] bytes = new byte[3];
  361.                 this.stream.Read(bytes, 0, 3);
  362.                 return Conversions.BytesToInt24(bytes);
  363.             }
  364.             catch (Exception ex)
  365.             {
  366.                 throw ex;
  367.             }
  368.         }
  369.  
  370.         public int ReadInt32()
  371.         {
  372.             try
  373.             {
  374.                 byte[] bytes = new byte[4];
  375.                 this.stream.Read(bytes, 0, 4);
  376.                 return Conversions.BytesToInt32(bytes);
  377.             }
  378.             catch (Exception ex)
  379.             {
  380.                 throw ex;
  381.             }
  382.         }
  383.  
  384.         public long ReadInt40()
  385.         {
  386.             try
  387.             {
  388.                 byte[] bytes = new byte[5];
  389.                 this.stream.Read(bytes, 0, 5);
  390.                 return Conversions.BytesToInt40(bytes);
  391.             }
  392.             catch (Exception ex)
  393.             {
  394.                 throw ex;
  395.             }
  396.         }
  397.  
  398.         public long ReadInt48()
  399.         {
  400.             try
  401.             {
  402.                 byte[] bytes = new byte[6];
  403.                 this.stream.Read(bytes, 0, 6);
  404.                 return Conversions.BytesToInt48(bytes);
  405.             }
  406.             catch (Exception ex)
  407.             {
  408.                 throw ex;
  409.             }
  410.         }
  411.  
  412.         public long ReadInt56()
  413.         {
  414.             try
  415.             {
  416.                 byte[] bytes = new byte[7];
  417.                 this.stream.Read(bytes, 0, 7);
  418.                 return Conversions.BytesToInt56(bytes);
  419.             }
  420.             catch (Exception ex)
  421.             {
  422.                 throw ex;
  423.             }
  424.         }
  425.  
  426.         public long ReadInt64()
  427.         {
  428.             try
  429.             {
  430.                 byte[] bytes = new byte[8];
  431.                 this.stream.Read(bytes, 0, 8);
  432.                 return Conversions.BytesToInt64(bytes);
  433.             }
  434.             catch (Exception ex)
  435.             {
  436.                 throw ex;
  437.             }
  438.         }
  439.  
  440.         public float ReadSingle()
  441.         {
  442.             try
  443.             {
  444.                 byte[] bytes = new byte[4];
  445.                 this.stream.Read(bytes, 0, 4);
  446.                 return Conversions.BytesToSingle(bytes);
  447.             }
  448.             catch (Exception ex)
  449.             {
  450.                 throw ex;
  451.             }
  452.         }
  453.  
  454.         public ushort ReadUInt16()
  455.         {
  456.             try
  457.             {
  458.                 byte[] bytes = new byte[2];
  459.                 this.stream.Read(bytes, 0, 2);
  460.                 return Conversions.BytesToUInt16(bytes);
  461.             }
  462.             catch (Exception ex)
  463.             {
  464.                 throw ex;
  465.             }
  466.         }
  467.  
  468.         public uint ReadUInt32()
  469.         {
  470.             try
  471.             {
  472.                 byte[] bytes = new byte[4];
  473.                 this.stream.Read(bytes, 0, 4);
  474.                 return Conversions.BytesToUInt32(bytes);
  475.             }
  476.             catch (Exception ex)
  477.             {
  478.                 throw ex;
  479.             }
  480.         }
  481.  
  482.         public ulong ReadUInt64()
  483.         {
  484.             try
  485.             {
  486.                 byte[] bytes = new byte[8];
  487.                 this.stream.Read(bytes, 0, 8);
  488.                 return Conversions.BytesToUInt64(bytes);
  489.             }
  490.             catch (Exception ex)
  491.             {
  492.                 throw ex;
  493.             }
  494.         }
  495.  
  496.         #endregion
  497.  
  498.         #region Writer
  499.  
  500.         public void Write(byte[] value)
  501.         {
  502.             try
  503.             {
  504.                 if (this.endian == Endian.Big)
  505.                 {
  506.                     this.SwapByteSex(value);
  507.                 }
  508.                 this.stream.Write(value, 0, value.Length);
  509.             }
  510.             catch (Exception ex)
  511.             {
  512.                 throw ex;
  513.             }
  514.         }
  515.  
  516.         public void Write(byte[] value, Endian _endian)
  517.         {
  518.             try
  519.             {
  520.                 if (_endian == Endian.Big)
  521.                 {
  522.                     this.SwapByteSex(value);
  523.                 }
  524.                 this.stream.Write(value, 0, value.Length);
  525.             }
  526.             catch (Exception ex)
  527.             {
  528.                 throw ex;
  529.             }
  530.         }
  531.  
  532.         public void Write(byte[] value, int count)
  533.         {
  534.             try
  535.             {
  536.                 if (this.endian == Endian.Big)
  537.                 {
  538.                     this.SwapByteSex(value);
  539.                 }
  540.                 this.stream.Write(value, 0, count);
  541.             }
  542.             catch (Exception ex)
  543.             {
  544.                 throw ex;
  545.             }
  546.         }
  547.  
  548.         public void Write(byte[] value, Endian _endian, int offset)
  549.         {
  550.             try
  551.             {
  552.                 if (_endian == Endian.Big)
  553.                 {
  554.                     this.SwapByteSex(value);
  555.                 }
  556.                 this.stream.Write(value, offset, value.Length);
  557.             }
  558.             catch (Exception ex)
  559.             {
  560.                 throw ex;
  561.             }
  562.         }
  563.  
  564.         public void Write(byte[] value, int count, Endian _endian)
  565.         {
  566.             try
  567.             {
  568.                 if (_endian == Endian.Big)
  569.                 {
  570.                     this.SwapByteSex(value);
  571.                 }
  572.                 this.stream.Write(value, 0, count);
  573.             }
  574.             catch (Exception ex)
  575.             {
  576.                 throw ex;
  577.             }
  578.         }
  579.  
  580.         public void Write(byte[] value, int offset, int count, Endian _endian)
  581.         {
  582.             try
  583.             {
  584.                 if (_endian == Endian.Big)
  585.                 {
  586.                     this.SwapByteSex(value);
  587.                 }
  588.                 this.stream.Write(value, offset, count);
  589.             }
  590.             catch (Exception ex)
  591.             {
  592.                 throw ex;
  593.             }
  594.         }
  595.  
  596.         public void WriteASCIIString(string value)
  597.         {
  598.             try
  599.             {
  600.                 byte[] bytes = Conversions.StringToBytes(value);
  601.                 this.Write(bytes);
  602.             }
  603.             catch (Exception ex)
  604.             {
  605.                 throw ex;
  606.             }
  607.         }
  608.  
  609.         public void Write(bool value)
  610.         {
  611.             try
  612.             {
  613.                 byte[] bytes = Conversions.BooleanToBytes(value);
  614.                 this.Write(bytes);
  615.             }
  616.             catch (Exception ex)
  617.             {
  618.                 throw ex;
  619.             }
  620.         }
  621.  
  622.         public void WriteByte(byte value)
  623.         {
  624.             try
  625.             {
  626.                 this.stream.WriteByte(value);
  627.             }
  628.             catch (Exception ex)
  629.             {
  630.                 throw ex;
  631.             }
  632.         }
  633.  
  634.         public void Write(char value)
  635.         {
  636.             try
  637.             {
  638.                 byte _byte = Convert.ToByte(value);
  639.                 this.WriteByte(_byte);
  640.             }
  641.             catch (Exception ex)
  642.             {
  643.                 throw ex;
  644.             }
  645.         }
  646.  
  647.         public void Write(double value)
  648.         {
  649.             try
  650.             {
  651.                 byte[] bytes = Conversions.DoubleToBytes(value);
  652.                 this.Write(bytes);
  653.             }
  654.             catch (Exception ex)
  655.             {
  656.                 throw ex;
  657.             }
  658.         }
  659.  
  660.         public void Write(short value)
  661.         {
  662.             try
  663.             {
  664.                 byte[] bytes = Conversions.Int16ToBytes(value);
  665.                 this.Write(bytes);
  666.             }
  667.             catch (Exception ex)
  668.             {
  669.                 throw ex;
  670.             }
  671.         }
  672.  
  673.         public void Write(int value)
  674.         {
  675.             try
  676.             {
  677.                 byte[] bytes = Conversions.Int32ToBytes(value);
  678.                 this.Write(bytes);
  679.             }
  680.             catch (Exception ex)
  681.             {
  682.                 throw ex;
  683.             }
  684.         }
  685.  
  686.         public void WriteInt40(long value)
  687.         {
  688.             try
  689.             {
  690.                 byte[] bytes = Conversions.Int40ToBytes(value);
  691.                 this.Write(bytes);
  692.             }
  693.             catch (Exception ex)
  694.             {
  695.                 throw ex;
  696.             }
  697.         }
  698.  
  699.         public void Write(long value)
  700.         {
  701.             try
  702.             {
  703.                 byte[] bytes = Conversions.Int64ToBytes(value);
  704.                 this.Write(bytes);
  705.             }
  706.             catch (Exception ex)
  707.             {
  708.                 throw ex;
  709.             }
  710.         }
  711.  
  712.         public void Write(sbyte value)
  713.         {
  714.             try
  715.             {
  716.                 byte bytes = Convert.ToByte(value);
  717.                 this.WriteByte(bytes);
  718.             }
  719.             catch (Exception ex)
  720.             {
  721.                 throw ex;
  722.             }
  723.         }
  724.  
  725.         public void Write(float value)
  726.         {
  727.             try
  728.             {
  729.                 byte[] bytes = Conversions.SingleToBytes(value);
  730.                 this.Write(bytes);
  731.             }
  732.             catch (Exception ex)
  733.             {
  734.                 throw ex;
  735.             }
  736.         }
  737.  
  738.         public void Write(ushort value)
  739.         {
  740.             try
  741.             {
  742.                 byte[] bytes = Conversions.UInt16ToBytes(value);
  743.                 this.Write(bytes);
  744.             }
  745.             catch (Exception ex)
  746.             {
  747.                 throw ex;
  748.             }
  749.         }
  750.  
  751.         public void Write(uint value)
  752.         {
  753.             try
  754.             {
  755.                 byte[] bytes = Conversions.UInt32ToBytes(value);
  756.                 this.Write(bytes);
  757.             }
  758.             catch (Exception ex)
  759.             {
  760.                 throw ex;
  761.             }
  762.         }
  763.  
  764.         public void Write(ulong value)
  765.         {
  766.             try
  767.             {
  768.                 byte[] bytes = Conversions.UInt64ToBytes(value);
  769.                 this.Write(bytes);
  770.             }
  771.             catch (Exception ex)
  772.             {
  773.                 throw ex;
  774.             }
  775.         }
  776.  
  777.         public void WriteUnicodeString(String str)
  778.         {
  779.             try
  780.             {
  781.                 int Length = str.Length;
  782.                 for (int i = 0; i < Length; i++)
  783.                 {
  784.                     this.Write(Conversions.CharToBytes(str[i]));
  785.                     if (Length - 1 > i)
  786.                         this.Write(Conversions.CharToBytes('\0'));
  787.                 }
  788.             }
  789.             catch (Exception ex)
  790.             {
  791.                 throw ex;
  792.             }
  793.         }
  794.  
  795.         #endregion
  796.  
  797.         #region Converter
  798.  
  799.         internal static class Conversions
  800.         {
  801.             #region BytesToType
  802.  
  803.             public static Int16 BytesToInt16(Byte[] bytes)
  804.             {
  805.                 if (bytes.Length == 2)
  806.                 {
  807.                     try
  808.                     {
  809.                         Int16 intt = BitConverter.ToInt16(bytes, 0);
  810.                         return intt;
  811.                     }
  812.                     catch (Exception ex)
  813.                     {
  814.                         throw ex;
  815.                     }
  816.                 }
  817.                 else
  818.                     throw new System.InvalidOperationException("Invalid value size.");
  819.             }
  820.  
  821.  
  822.             public static UInt16 BytesToUInt16(Byte[] bytes)
  823.             {
  824.                 if (bytes.Length == 2)
  825.                 {
  826.                     try
  827.                     {
  828.                         UInt16 intt = BitConverter.ToUInt16(bytes, 0);
  829.                         return intt;
  830.                     }
  831.                     catch (Exception ex)
  832.                     {
  833.                         throw ex;
  834.                     }
  835.                 }
  836.                 else
  837.                     throw new System.InvalidOperationException("Invalid value size.");
  838.             }
  839.  
  840.             public static Int32 BytesToInt24(Byte[] bytes) //Byte[] must be 3 bytes in size
  841.             {
  842.                 if (bytes.Length == 3)
  843.                 {
  844.                     try
  845.                     {
  846.                         return ((bytes[2] << 16
  847.                             | bytes[1] << 8
  848.                             | bytes[0]));
  849.                     }
  850.                     catch (Exception ex)
  851.                     {
  852.                         throw ex;
  853.                     }
  854.                 }
  855.                 else
  856.                     throw new System.InvalidOperationException("Invalid value size.");
  857.             }
  858.  
  859.             public static Int32 BytesToInt32(Byte[] bytes)
  860.             {
  861.                 if (bytes.Length == 4)
  862.                 {
  863.                     try
  864.                     {
  865.                         Int32 intt = BitConverter.ToInt32(bytes, 0);
  866.                         return intt;
  867.                     }
  868.                     catch (Exception ex)
  869.                     {
  870.                         throw ex;
  871.                     }
  872.                 }
  873.                 else
  874.                     throw new System.InvalidOperationException("Invalid value size.");
  875.             }
  876.  
  877.             public static UInt32 BytesToUInt32(Byte[] bytes)
  878.             {
  879.                 if (bytes.Length == 4)
  880.                 {
  881.                     try
  882.                     {
  883.                         UInt32 intt = BitConverter.ToUInt32(bytes, 0);
  884.                         return intt;
  885.                     }
  886.                     catch (Exception ex)
  887.                     {
  888.                         throw ex;
  889.                     }
  890.                 }
  891.                 else
  892.                     throw new System.InvalidOperationException("Invalid value size.");
  893.             }
  894.  
  895.             public static Int64 BytesToInt40(Byte[] bytes) //Byte[] must be 5 bytes in size
  896.             {
  897.                 if (bytes.Length == 5)
  898.                 {
  899.                     try
  900.                     {
  901.                         return ((bytes[4] << 32
  902.                             | bytes[3] << 24
  903.                             | bytes[2] << 16
  904.                             | bytes[1] << 8
  905.                             | bytes[0]));
  906.                     }
  907.                     catch (Exception ex)
  908.                     {
  909.                         throw ex;
  910.                     }
  911.                 }
  912.                 else
  913.                     throw new System.InvalidOperationException("Invalid value size.");
  914.             }
  915.  
  916.             public static Int64 BytesToInt48(Byte[] bytes) //Byte[] must be 6 bytes in size
  917.             {
  918.                 if (bytes.Length == 6)
  919.                 {
  920.                     try
  921.                     {
  922.                         return ((bytes[5] << 40
  923.                             | bytes[4] << 32
  924.                             | bytes[3] << 24
  925.                             | bytes[2] << 16
  926.                             | bytes[1] << 8
  927.                             | bytes[0]));
  928.                     }
  929.                     catch (Exception ex)
  930.                     {
  931.                         throw ex;
  932.                     }
  933.                 }
  934.                 else
  935.                     throw new System.InvalidOperationException("Invalid value size.");
  936.             }
  937.  
  938.             public static Int64 BytesToInt56(Byte[] bytes) //Byte[] must be 7 bytes in size
  939.             {
  940.                 if (bytes.Length == 7)
  941.                 {
  942.                     try
  943.                     {
  944.                         return ((bytes[6] << 48
  945.                             | bytes[5] << 40
  946.                             | bytes[4] << 32
  947.                             | bytes[3] << 24
  948.                             | bytes[2] << 16
  949.                             | bytes[1] << 8
  950.                             | bytes[0]));
  951.                     }
  952.                     catch (Exception ex)
  953.                     {
  954.                         throw ex;
  955.                     }
  956.                 }
  957.                 else
  958.                     throw new System.InvalidOperationException("Invalid value size.");
  959.             }
  960.  
  961.             public static Int64 BytesToInt64(Byte[] bytes)
  962.             {
  963.                 if (bytes.Length == 8)
  964.                 {
  965.                     try
  966.                     {
  967.                         Int64 intt = BitConverter.ToInt64(bytes, 0);
  968.                         return intt;
  969.                     }
  970.                     catch (Exception ex)
  971.                     {
  972.                         throw ex;
  973.                     }
  974.                 }
  975.                 else
  976.                     throw new System.InvalidOperationException("Invalid value size.");
  977.             }
  978.  
  979.             public static UInt64 BytesToUInt64(Byte[] bytes)
  980.             {
  981.                 if (bytes.Length == 8)
  982.                 {
  983.                     try
  984.                     {
  985.                         UInt64 intt = BitConverter.ToUInt64(bytes, 0);
  986.                         return intt;
  987.                     }
  988.                     catch (Exception ex)
  989.                     {
  990.                         throw ex;
  991.                     }
  992.                 }
  993.                 else
  994.                     throw new System.InvalidOperationException("Invalid value size.");
  995.             }
  996.  
  997.             public static Double BytesToDouble(Byte[] bytes)
  998.             {
  999.                 if (bytes.Length == 8)
  1000.                 {
  1001.                     try
  1002.                     {
  1003.                         Double dub = BitConverter.ToDouble(bytes, 0);
  1004.                         return dub;
  1005.                     }
  1006.                     catch (Exception ex)
  1007.                     {
  1008.                         throw ex;
  1009.                     }
  1010.                 }
  1011.                 else
  1012.                     throw new System.InvalidOperationException("Invalid value size.");
  1013.             }
  1014.  
  1015.             public static Single BytesToSingle(Byte[] bytes)
  1016.             {
  1017.                 if (bytes.Length == 4)
  1018.                 {
  1019.  
  1020.                     Single dub = BitConverter.ToSingle(bytes, 0);
  1021.                     return dub;
  1022.                 }
  1023.                 else
  1024.                     throw new System.InvalidOperationException("Invalid value size.");
  1025.             }
  1026.  
  1027.             public static Boolean BytesToBoolean(Byte _byte)
  1028.             {
  1029.                 try
  1030.                 {
  1031.                     Boolean boolz = BitConverter.ToBoolean(new byte[] { _byte }, 0);
  1032.                     return boolz;
  1033.                 }
  1034.                 catch (Exception ex)
  1035.                 {
  1036.                     throw ex;
  1037.                 }
  1038.             }
  1039.  
  1040.             public static String BytesToString(Byte[] bytes)
  1041.             {
  1042.                 try
  1043.                 {
  1044.                     String str = BitConverter.ToString(bytes);
  1045.                     return str;
  1046.                 }
  1047.                 catch (Exception ex)
  1048.                 {
  1049.                     throw ex;
  1050.                 }
  1051.             }
  1052.  
  1053.             #endregion
  1054.  
  1055.             #region TypeToBytes
  1056.  
  1057.             public static byte[] Int16ToBytes(Int16 value)
  1058.             {
  1059.                 try
  1060.                 {
  1061.                     byte[] bytes = BitConverter.GetBytes(value);
  1062.                     return bytes;
  1063.                 }
  1064.                 catch (Exception ex)
  1065.                 {
  1066.                     throw ex;
  1067.                 }
  1068.             }
  1069.  
  1070.             public static byte[] UInt16ToBytes(UInt16 value)
  1071.             {
  1072.                 try
  1073.                 {
  1074.                     byte[] bytes = BitConverter.GetBytes(value);
  1075.                     return bytes;
  1076.                 }
  1077.                 catch (Exception ex)
  1078.                 {
  1079.                     throw ex;
  1080.                 }
  1081.             }
  1082.  
  1083.             public static byte[] Int24ToBytes(Int32 value)
  1084.             {
  1085.                 if (8388607 >= value && -8388607 <= value)
  1086.                 {
  1087.                     try
  1088.                     {
  1089.                         Byte[] bytes = BitConverter.GetBytes(value);
  1090.                         Byte[] newBytes = { bytes[0], bytes[1], bytes[2] };
  1091.                         return newBytes;
  1092.                     }
  1093.                     catch (Exception ex)
  1094.                     {
  1095.                         throw ex;
  1096.                     }
  1097.                 }
  1098.                 else
  1099.                     throw new System.InvalidOperationException("Invalid value size.");
  1100.             }
  1101.  
  1102.             public static byte[] Int32ToBytes(Int32 value)
  1103.             {
  1104.                 try
  1105.                 {
  1106.                     byte[] bytes = BitConverter.GetBytes(value);
  1107.                     return bytes;
  1108.                 }
  1109.                 catch (Exception ex)
  1110.                 {
  1111.                     throw ex;
  1112.                 }
  1113.             }
  1114.  
  1115.             public static byte[] UInt32ToBytes(UInt32 value)
  1116.             {
  1117.                 try
  1118.                 {
  1119.                     byte[] bytes = BitConverter.GetBytes(value);
  1120.                     return bytes;
  1121.                 }
  1122.                 catch (Exception ex)
  1123.                 {
  1124.                     throw ex;
  1125.                 }
  1126.             }
  1127.  
  1128.             public static byte[] Int40ToBytes(Int64 value)
  1129.             {
  1130.                 if (549755813887 >= value && -549755813887 <= value)
  1131.                 {
  1132.                     try
  1133.                     {
  1134.                         Byte[] bytes = BitConverter.GetBytes(value);
  1135.                         Byte[] newBytes = { bytes[0], bytes[1], bytes[2], bytes[3], bytes[4] };
  1136.                         return newBytes;
  1137.                     }
  1138.                     catch (Exception ex)
  1139.                     {
  1140.                         throw ex;
  1141.                     }
  1142.                 }
  1143.                 else
  1144.                     throw new System.InvalidOperationException("Invalid value size.");
  1145.  
  1146.             }
  1147.  
  1148.             public static byte[] Int48ToBytes(Int64 value)
  1149.             {
  1150.                 if (140737488355327 >= value && -140737488355327 <= value)
  1151.                 {
  1152.                     try
  1153.                     {
  1154.                         Byte[] bytes = BitConverter.GetBytes(value);
  1155.                         Byte[] newBytes = { bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5] };
  1156.                         return newBytes;
  1157.                     }
  1158.                     catch (Exception ex)
  1159.                     {
  1160.                         throw ex;
  1161.                     }
  1162.                 }
  1163.                 else
  1164.                     throw new System.InvalidOperationException("Invalid value size.");
  1165.             }
  1166.  
  1167.             public static byte[] Int56ToBytes(Int64 value)
  1168.             {
  1169.                 if (36028797018963967 >= value && -36028797018963967 <= value)
  1170.                 {
  1171.                     try
  1172.                     {
  1173.                         Byte[] bytes = BitConverter.GetBytes(value);
  1174.                         Byte[] newBytes = { bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6] };
  1175.                         return newBytes;
  1176.                     }
  1177.                     catch (Exception ex)
  1178.                     {
  1179.                         throw ex;
  1180.                     }
  1181.                 }
  1182.                 else
  1183.                     throw new System.InvalidOperationException("Invalid value size.");
  1184.             }
  1185.  
  1186.             public static byte[] Int64ToBytes(Int64 value)
  1187.             {
  1188.                 try
  1189.                 {
  1190.                     byte[] bytes = BitConverter.GetBytes(value);
  1191.                     return bytes;
  1192.                 }
  1193.                 catch (Exception ex)
  1194.                 {
  1195.                     throw ex;
  1196.                 }
  1197.             }
  1198.  
  1199.             public static byte[] UInt64ToBytes(UInt64 value)
  1200.             {
  1201.                 try
  1202.                 {
  1203.                     byte[] bytes = BitConverter.GetBytes(value);
  1204.                     return bytes;
  1205.                 }
  1206.                 catch (Exception ex)
  1207.                 {
  1208.                     throw ex;
  1209.                 }
  1210.             }
  1211.  
  1212.             public static byte[] DoubleToBytes(Double value)
  1213.             {
  1214.                 try
  1215.                 {
  1216.                     byte[] bytes = BitConverter.GetBytes(value);
  1217.                     return bytes;
  1218.                 }
  1219.                 catch (Exception ex)
  1220.                 {
  1221.                     throw ex;
  1222.                 }
  1223.             }
  1224.  
  1225.             public static byte[] SingleToBytes(Single value)
  1226.             {
  1227.                 try
  1228.                 {
  1229.                     byte[] bytes = BitConverter.GetBytes(value);
  1230.                     return bytes;
  1231.                 }
  1232.                 catch (Exception ex)
  1233.                 {
  1234.                     throw ex;
  1235.                 }
  1236.             }
  1237.  
  1238.             public static byte[] BooleanToBytes(Boolean value)
  1239.             {
  1240.                 try
  1241.                 {
  1242.                     byte[] bytes = BitConverter.GetBytes(value);
  1243.                     return bytes;
  1244.                 }
  1245.                 catch (Exception ex)
  1246.                 {
  1247.                     throw ex;
  1248.                 }
  1249.             }
  1250.  
  1251.             public static byte[] CharToBytes(Char value)
  1252.             {
  1253.                 try
  1254.                 {
  1255.                     byte[] bytes = BitConverter.GetBytes(value);
  1256.                     return bytes;
  1257.                 }
  1258.                 catch (Exception ex)
  1259.                 {
  1260.                     throw ex;
  1261.                 }
  1262.             }
  1263.  
  1264.             public static byte[] StringToBytes(String value)
  1265.             {
  1266.                 try
  1267.                 {
  1268.                     char[] chars = value.ToCharArray();
  1269.                     byte[] bytes = new byte[chars.Length];
  1270.                     int counter = 0;
  1271.                     byte[] temp;
  1272.                     foreach (char c in chars)
  1273.                     {
  1274.                         temp = BitConverter.GetBytes(c);
  1275.                         bytes[counter] = temp[0];
  1276.                         counter++;
  1277.                     }
  1278.                     return bytes;
  1279.                 }
  1280.                 catch (Exception ex)
  1281.                 {
  1282.                     throw ex;
  1283.                 }
  1284.             }
  1285.             #endregion
  1286.         }
  1287.  
  1288.         #endregion
  1289.  
  1290.         #region Special Writer
  1291.         public void Write(Object value, SpecialInts _intType)
  1292.         {
  1293.             try
  1294.             {
  1295.                 if (_intType == SpecialInts.Int24)
  1296.                 {
  1297.                     byte[] bytes = Conversions.Int24ToBytes((Int32)value);
  1298.                     this.Write(bytes);
  1299.                 }
  1300.                 else if (_intType == SpecialInts.Int40)
  1301.                 {
  1302.                     byte[] bytes = Conversions.Int48ToBytes((Int64)value);
  1303.                     this.Write(bytes);
  1304.                 }
  1305.                 else if (_intType == SpecialInts.Int48)
  1306.                 {
  1307.                     byte[] bytes = Conversions.Int48ToBytes((Int64)value);
  1308.                     this.Write(bytes);
  1309.                 }
  1310.                 else if (_intType == SpecialInts.Int56)
  1311.                 {
  1312.                     byte[] bytes = Conversions.Int56ToBytes((Int64)value);
  1313.                     this.Write(bytes);
  1314.                 }
  1315.             }
  1316.             catch (Exception ex)
  1317.             {
  1318.                 throw ex;
  1319.             }
  1320.         }
  1321.            
  1322.         #endregion
  1323.     }
  1324.  
  1325.     public enum Endian
  1326.     {
  1327.         Big,
  1328.         Little
  1329.     }
  1330.     public enum SpecialInts
  1331.     {
  1332.         Int24,
  1333.         Int40,
  1334.         Int48,
  1335.         Int56
  1336.     }
  1337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement