Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Binary reader in Big Endian?

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.

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?

Any help is appreciated.

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

like image 506
mowwwalker Avatar asked Dec 23 '11 21:12

mowwwalker


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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

like image 193
mowwwalker Avatar answered Oct 15 '22 01:10

mowwwalker