Advertisement
PrimeNotorious

BigEndianReader

May 3rd, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace YourProgramName
  6. {
  7. class BigEndianReader : BinaryReader
  8.     {
  9.         private byte[] a16 = new byte[2];
  10.         private byte[] a32 = new byte[4];
  11.         private byte[] a64 = new byte[8];
  12.  
  13.         public BigEndianReader(Stream stream) : base(stream) { }
  14.  
  15.         public string ReadString()
  16.         {
  17.             char c;
  18.             uint count = 0;
  19.             List<char> chars = new List<char>();
  20.             while ((c = ReadChar()) != 0)
  21.             {
  22.                 chars.Add(c);
  23.                 count++;
  24.             }
  25.             return new string(chars.ToArray());
  26.         }
  27.  
  28.         public override Int16 ReadInt16()
  29.         {
  30.             a16 = base.ReadBytes(2);
  31.             Array.Reverse(a16);
  32.             return BitConverter.ToInt16(a16, 0);
  33.         }
  34.  
  35.         public override int ReadInt32()
  36.         {
  37.             a32 = base.ReadBytes(4);
  38.             Array.Reverse(a32);
  39.             return BitConverter.ToInt32(a32, 0);
  40.         }
  41.  
  42.         public override Int64 ReadInt64()
  43.         {
  44.             a64 = base.ReadBytes(8);
  45.             Array.Reverse(a64);
  46.             return BitConverter.ToInt64(a64, 0);
  47.         }
  48.  
  49.         public override UInt16 ReadUInt16()
  50.         {
  51.             a16 = base.ReadBytes(2);
  52.             Array.Reverse(a16);
  53.             return BitConverter.ToUInt16(a16, 0);
  54.         }
  55.  
  56.         public override UInt32 ReadUInt32()
  57.         {
  58.             a32 = base.ReadBytes(4);
  59.             Array.Reverse(a32);
  60.             return BitConverter.ToUInt32(a32, 0);
  61.         }
  62.  
  63.         public override UInt64 ReadUInt64()
  64.         {
  65.             a64 = base.ReadBytes(8);
  66.             Array.Reverse(a64);
  67.             return BitConverter.ToUInt64(a64, 0);
  68.         }
  69.     }
  70.  
  71.     public class BigEndianWriter : BinaryWriter
  72.     {
  73.         private byte[] a16 = new byte[2];
  74.         private byte[] a32 = new byte[4];
  75.         private byte[] a64 = new byte[8];
  76.  
  77.         public BigEndianWriter(Stream output)
  78.             : base(output)
  79.         {
  80.         }
  81.  
  82.         public override void Write(Int16 value)
  83.         {
  84.             a16 = BitConverter.GetBytes(value);
  85.             Array.Reverse(a16);
  86.             base.Write(a16);
  87.         }
  88.  
  89.         public override void Write(Int32 value)
  90.         {
  91.             a32 = BitConverter.GetBytes(value);
  92.             Array.Reverse(a32);
  93.             base.Write(a32);
  94.         }
  95.  
  96.         public override void Write(Int64 value)
  97.         {
  98.             a64 = BitConverter.GetBytes(value);
  99.             Array.Reverse(a64);
  100.             base.Write(a64);
  101.         }
  102.  
  103.         public override void Write(UInt16 value)
  104.         {
  105.             a16 = BitConverter.GetBytes(value);
  106.             Array.Reverse(a16);
  107.             base.Write(a16);
  108.         }
  109.  
  110.         public override void Write(UInt32 value)
  111.         {
  112.             a32 = BitConverter.GetBytes(value);
  113.             Array.Reverse(a32);
  114.             base.Write(a32);
  115.         }
  116.  
  117.         public override void Write(UInt64 value)
  118.         {
  119.             a64 = BitConverter.GetBytes(value);
  120.             Array.Reverse(a64);
  121.             base.Write(a64);
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement