Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace YourProgramName
- {
- class BigEndianReader : BinaryReader
- {
- private byte[] a16 = new byte[2];
- private byte[] a32 = new byte[4];
- private byte[] a64 = new byte[8];
- public BigEndianReader(Stream stream) : base(stream) { }
- public string ReadString()
- {
- char c;
- uint count = 0;
- List<char> chars = new List<char>();
- while ((c = ReadChar()) != 0)
- {
- chars.Add(c);
- count++;
- }
- return new string(chars.ToArray());
- }
- public override Int16 ReadInt16()
- {
- a16 = base.ReadBytes(2);
- Array.Reverse(a16);
- return BitConverter.ToInt16(a16, 0);
- }
- public override int ReadInt32()
- {
- a32 = base.ReadBytes(4);
- Array.Reverse(a32);
- return BitConverter.ToInt32(a32, 0);
- }
- public override Int64 ReadInt64()
- {
- a64 = base.ReadBytes(8);
- Array.Reverse(a64);
- return BitConverter.ToInt64(a64, 0);
- }
- public override UInt16 ReadUInt16()
- {
- a16 = base.ReadBytes(2);
- Array.Reverse(a16);
- return BitConverter.ToUInt16(a16, 0);
- }
- public override UInt32 ReadUInt32()
- {
- a32 = base.ReadBytes(4);
- Array.Reverse(a32);
- return BitConverter.ToUInt32(a32, 0);
- }
- public override UInt64 ReadUInt64()
- {
- a64 = base.ReadBytes(8);
- Array.Reverse(a64);
- return BitConverter.ToUInt64(a64, 0);
- }
- }
- public class BigEndianWriter : BinaryWriter
- {
- private byte[] a16 = new byte[2];
- private byte[] a32 = new byte[4];
- private byte[] a64 = new byte[8];
- public BigEndianWriter(Stream output)
- : base(output)
- {
- }
- public override void Write(Int16 value)
- {
- a16 = BitConverter.GetBytes(value);
- Array.Reverse(a16);
- base.Write(a16);
- }
- public override void Write(Int32 value)
- {
- a32 = BitConverter.GetBytes(value);
- Array.Reverse(a32);
- base.Write(a32);
- }
- public override void Write(Int64 value)
- {
- a64 = BitConverter.GetBytes(value);
- Array.Reverse(a64);
- base.Write(a64);
- }
- public override void Write(UInt16 value)
- {
- a16 = BitConverter.GetBytes(value);
- Array.Reverse(a16);
- base.Write(a16);
- }
- public override void Write(UInt32 value)
- {
- a32 = BitConverter.GetBytes(value);
- Array.Reverse(a32);
- base.Write(a32);
- }
- public override void Write(UInt64 value)
- {
- a64 = BitConverter.GetBytes(value);
- Array.Reverse(a64);
- base.Write(a64);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement