DarkScript
Enthusiast
Simple IO i made,
Supports:
reading a byte[] of endianness little, big and both styles of mixed,
Writing ints, int64, and byte[]'s as endianness little, big and both styles of mixed,
To read in a different endianness use the EdBinaryReader.ReadBytes method and it will be the second overload.
Simply add all 4 classes/enum to your project and your ready to go, namespace for all is "EdIO.IO"
P.S. All data is stored as big endian after being read, or if your writing it is big endian until you write, then its converted.
[This is fully functional however has limited functions but can easily be built upon]
Sources:
Reader
Writer
Endian Enum
Endian Converter
Supports:
reading a byte[] of endianness little, big and both styles of mixed,
Writing ints, int64, and byte[]'s as endianness little, big and both styles of mixed,
To read in a different endianness use the EdBinaryReader.ReadBytes method and it will be the second overload.
Simply add all 4 classes/enum to your project and your ready to go, namespace for all is "EdIO.IO"
P.S. All data is stored as big endian after being read, or if your writing it is big endian until you write, then its converted.
[This is fully functional however has limited functions but can easily be built upon]
Sources:
Reader
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace EdIO.IO
{
public class EdBinaryReader : BinaryReader
{
public EdBinaryReader(FileStream fs) : base(fs) { }
public byte[] ReadBytes(int count, Endian c)
{
byte[] y = this.ReadBytes(count);
Array.Reverse(y);
return EndianSorter.ConvertFromBig(y, c);
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace EdIO.IO
{
public class EdBinaryWriter : BinaryWriter
{
public EdBinaryWriter(FileStream fs) : base(fs)
{
}
public void WriteEndian(byte[] x, Endian v)
{
Write(EndianSorter.ConvertFromBig(x, v));
}
public void WriteEndian(int x, Endian v)
{
byte[] intBytes = BitConverter.GetBytes(x);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(intBytes);
}
Write(EndianSorter.ConvertFromBig(intBytes, v));
}
public void WriteEndian(Int64 x, Endian v)
{
byte[] intBytes = BitConverter.GetBytes(x);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(intBytes);
}
Write(EndianSorter.ConvertFromBig(intBytes, v));
}
public void WriteEndian(string x, Endian v)
{
Write(EndianSorter.ConvertFromBig(Encoding.UTF8.GetBytes(x), v));
}
}
}
Endian Enum
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EdIO.IO
{
public enum Endian
{
Big,
Small,
Mixed_BigFirst,
Mixed_SmallFirst
}
}
Endian Converter
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EdIO.IO
{
public static class EndianSorter
{
public static byte[] ConvertFromBig(byte[] x, Endian To)
{
byte[] cache = x;
switch (To)
{
case Endian.Big:
break;
case Endian.Small:
Array.Reverse(cache);
break;
case Endian.Mixed_BigFirst:
Array.Reverse(cache, (cache.Length / 2), (cache.Length / 2));
break;
case Endian.Mixed_SmallFirst:
Array.Reverse(cache, 0, (cache.Length / 2));
break;
}
return cache;
}
}
}
Last edited: