What's new

.NET [csharp] EdIO [Supports little, big and mixed Endianness]

  • Thread starter DarkScript
  • Start date
  • Views 870
DarkScript

DarkScript

Enthusiast
Messages
53
Reaction score
7
Points
55
Sin$
7
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
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);
        }
    }
}
Writer
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:
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
Simple IO i made

I haven't tried your code, but I believe you're converting the bytes wrong. BinaryReader automatically reads bytes in little endian, and in your IO if you chose "Endian.Big" as your choice, it would not reverse the bytes. The same goes for "Endian.Small"; since you're flipping the bytes once you read them, they become big endian, so all you need to do is switch them in the conversion class.
 
Last edited:
DarkScript

DarkScript

Enthusiast
Messages
53
Reaction score
7
Points
55
Sin$
7
I haven't tried your code, but I believe you're converting the bytes wrong. BinaryReader automatically reads bytes in little endian, and in your IO if you chose "Endian.Big" as your choice, it would not reverse the bytes. The same goes for "Endian.Small"; since you're flipping the bytes once you read them, they become big endian, so all you need to do is switch them in the conversion class.

try the code, i checked the endianness after making it and it worked fine for me. 
okay i read your message wrong, thanks man :smile:
 
Top Bottom
Login
Register