Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace Tico.Filesystem
- {
- class EndianBinaryReader : BinaryReader
- {
- public EndianBinaryReader(Stream s) : base(s)
- {}
- public EndianBinaryReader(Stream s, Encoding e) : base(s, e)
- {}
- public void SetEndianess(Endianess endi)
- {
- if (endi == Endianess.BigEndian)
- isBigEndian = true;
- else
- isBigEndian = false;
- }
- public override short ReadInt16()
- {
- UInt16 inputVal = base.ReadUInt16();
- if (isBigEndian)
- return (Int16)((inputVal >> 8) | (inputVal << 8));
- else
- return (Int16)inputVal;
- }
- public override int ReadInt32()
- {
- UInt32 inputVal = base.ReadUInt32();
- if (isBigEndian)
- return (Int32)((inputVal >> 24) | ((inputVal & 0xFF0000) >> 8) | ((inputVal & 0xFF00) << 8) | (inputVal << 24));
- else
- return (Int32)inputVal;
- }
- public override ushort ReadUInt16()
- {
- Int16 inputVal = base.ReadInt16();
- if (isBigEndian)
- return (UInt16)((inputVal >> 8) | (inputVal << 8));
- else
- return (UInt16)inputVal;
- }
- public override uint ReadUInt32()
- {
- Int32 inputVal = base.ReadInt32();
- if (isBigEndian)
- return (UInt32)((inputVal >> 24) | ((inputVal & 0xFF0000) >> 8) | ((inputVal & 0xFF00) << 8) | (inputVal << 24));
- else
- return (UInt32)inputVal;
- }
- public string ReadString(int len)
- {
- return Encoding.ASCII.GetString(base.ReadBytes(len));
- }
- public override float ReadSingle()
- {
- byte[] inputBytes = base.ReadBytes(4);
- if (!isBigEndian)
- Array.Reverse(inputBytes);
- float floatVal = BitConverter.ToSingle(inputBytes, 0);
- return floatVal;
- }
- public override double ReadDouble()
- {
- byte[] inputBytes = base.ReadBytes(8);
- if (!isBigEndian)
- Array.Reverse(inputBytes);
- double doubleVal = BitConverter.ToDouble(inputBytes, 0);
- return doubleVal;
- }
- public enum Endianess
- {
- LittleEndian,
- BigEndian
- };
- bool isBigEndian;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement