Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Tico.Filesystem
  9. {
  10.     class EndianBinaryReader : BinaryReader
  11.     {
  12.         public EndianBinaryReader(Stream s) : base(s)
  13.         {}
  14.  
  15.         public EndianBinaryReader(Stream s, Encoding e) : base(s, e)
  16.         {}
  17.  
  18.         public void SetEndianess(Endianess endi)
  19.         {
  20.             if (endi == Endianess.BigEndian)
  21.                 isBigEndian = true;
  22.             else
  23.                 isBigEndian = false;
  24.         }
  25.  
  26.         public override short ReadInt16()
  27.         {
  28.             UInt16 inputVal = base.ReadUInt16();
  29.  
  30.             if (isBigEndian)
  31.                 return (Int16)((inputVal >> 8) | (inputVal << 8));
  32.             else
  33.                 return (Int16)inputVal;
  34.         }
  35.  
  36.         public override int ReadInt32()
  37.         {
  38.             UInt32 inputVal = base.ReadUInt32();
  39.  
  40.             if (isBigEndian)
  41.                 return (Int32)((inputVal >> 24) | ((inputVal & 0xFF0000) >> 8) | ((inputVal & 0xFF00) << 8) | (inputVal << 24));
  42.             else
  43.                 return (Int32)inputVal;
  44.         }
  45.  
  46.         public override ushort ReadUInt16()
  47.         {
  48.             Int16 inputVal = base.ReadInt16();
  49.  
  50.             if (isBigEndian)
  51.                 return (UInt16)((inputVal >> 8) | (inputVal << 8));
  52.             else
  53.                 return (UInt16)inputVal;
  54.         }
  55.  
  56.         public override uint ReadUInt32()
  57.         {
  58.             Int32 inputVal = base.ReadInt32();
  59.  
  60.             if (isBigEndian)
  61.                 return (UInt32)((inputVal >> 24) | ((inputVal & 0xFF0000) >> 8) | ((inputVal & 0xFF00) << 8) | (inputVal << 24));
  62.             else
  63.                 return (UInt32)inputVal;
  64.         }
  65.  
  66.         public string ReadString(int len)
  67.         {
  68.             return Encoding.ASCII.GetString(base.ReadBytes(len));
  69.         }
  70.  
  71.         public override float ReadSingle()
  72.         {
  73.             byte[] inputBytes = base.ReadBytes(4);
  74.  
  75.             if (!isBigEndian)
  76.                 Array.Reverse(inputBytes);
  77.  
  78.             float floatVal = BitConverter.ToSingle(inputBytes, 0);
  79.             return floatVal;
  80.         }
  81.  
  82.         public override double ReadDouble()
  83.         {
  84.             byte[] inputBytes = base.ReadBytes(8);
  85.  
  86.             if (!isBigEndian)
  87.                 Array.Reverse(inputBytes);
  88.  
  89.             double doubleVal = BitConverter.ToDouble(inputBytes, 0);
  90.             return doubleVal;
  91.         }
  92.  
  93.         public enum Endianess
  94.         {
  95.             LittleEndian,
  96.             BigEndian
  97.         };
  98.  
  99.         bool isBigEndian;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement