C# - Binary reader in Big Endian?

后端 未结 6 855
有刺的猬
有刺的猬 2020-12-02 18:38

I\'m trying to improve my understanding of the STFS file format by using a program to read all the different bits of information. Using a website with a reference of which o

6条回答
  •  日久生厌
    2020-12-02 19:12

    I'm not usually one to answer my own questions, but I've accomplished exactly what I wanted with some simple code:

    class BinaryReader2 : BinaryReader { 
        public BinaryReader2(System.IO.Stream stream)  : base(stream) { }
    
        public override int ReadInt32()
        {
            var data = base.ReadBytes(4);
            Array.Reverse(data);
            return BitConverter.ToInt32(data, 0);
        }
    
        public Int16 ReadInt16()
        {
            var data = base.ReadBytes(2);
            Array.Reverse(data);
            return BitConverter.ToInt16(data, 0);
        }
    
        public Int64 ReadInt64()
        {
            var data = base.ReadBytes(8);
            Array.Reverse(data);
            return BitConverter.ToInt64(data, 0);
        }
    
        public UInt32 ReadUInt32()
        {
            var data = base.ReadBytes(4);
            Array.Reverse(data);
            return BitConverter.ToUInt32(data, 0);
        }
    
    }
    

    I knew that's what I wanted, but I didn't know how to write it. I found this page and it helped: http://www.codekeep.net/snippets/870c4ab3-419b-4dd2-a950-6d45beaf1295.aspx

提交回复
热议问题