C# - 大端二进制阅读器? [英] C# - Binary reader in Big Endian?

查看:194
本文介绍了C# - 大端二进制阅读器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个程序来读取信息的所有不同的位来提高我的STFS文件格式的理解。使用的网站与参考,其中偏移包含哪些信息,我写了一些code,有一个二进制读者通过该文件,并将值正确的变量。

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 offsets contain what information, I wrote some code that has a binary reader go through the file and place the values in the correct variables.

问题是,所有的数据都应该是大端,一切二元读者读,就是little endian。那么,什么是去修复最好的方法?

The problem is that all the data is SUPPOSED to be Big Endian, and everything the binary reader read is Little Endian. So, what's the best way to go about fixing this?

我可以创建一个模拟类,返回的字节数组颠倒二进制读者?有什么我可以在类实例改变,这将使它在大端读,所以我不必重写一切吗?

Can I create a mimic class of Binary reader that returns a reversed array of bytes? Is there something I can change in class instance that will make it read in big endian so I don't have to rewrite everything?

任何帮助是AP preciated。

Any help is appreciated.

编辑:我尝试添加Encoding.BigEndianUni code作为一个参数,但它仍然读小尾数

edit: I tried adding Encoding.BigEndianUnicode as a parameter, but it still reads little endian.

推荐答案

我不是一般人回答我自己的问题,但我已经完成正是我想要一些简单的code:

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 { 
    private byte[] a16 = new byte[2];
    private byte[] a32 = new byte[4];
    private byte[] a64 = new byte[8];
    public BinaryReader2(System.IO.Stream stream)  : base(stream) { }
    public override int ReadInt32()
    {
        a32 = base.ReadBytes(4);
        Array.Reverse(a32);
        return BitConverter.ToInt32(a32,0);
    }
    public Int16 ReadInt16()
    {
        a16 = base.ReadBytes(2);
        Array.Reverse(a16);
        return BitConverter.ToInt16(a16, 0);
    }
    public Int64 ReadInt64()
    {
        a64 = base.ReadBytes(8);
        Array.Reverse(a64);
        return BitConverter.ToInt64(a64, 0);
    }
    public UInt32 ReadUInt32()
    {
        a32 = base.ReadBytes(4);
        Array.Reverse(a32);
        return BitConverter.ToUInt32(a32, 0);
    }

}

我知道这就是我想要的,但我不知道怎么写。我发现这个页面,它帮助:<一href=\"http://www.$c$ckeep.net/snippets/870c4ab3-419b-4dd2-a950-6d45beaf1295.aspx\">http://www.$c$ckeep.net/snippets/870c4ab3-419b-4dd2-a950-6d45beaf1295.aspx

这篇关于C# - 大端二进制阅读器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆