Backwards Binary Reader?

G

Guest

Hi

I've just tried the following code

FileStream objFS = new FileStream(strFileName, FileMode.Open, FileAccess.Read)
BinaryReader objReader = new BinaryReader(objFS)

UInt32 intFirst = objReader.ReadUInt32()
UInt32 intSecond = 0xC5B8FF00

Now I know that the first four bytes in the file are C5 B8 FF 00 in that order, but when I compare intFirst with intSecond they don't match. On further investigation, intFirst contains 00FFB8C5. WTF?!?

Why is the Binary Reader reading the bytes in backwards? Is there a simple way to switch them the right way, or force the Binary Reader to read them properly

Burns x
 
J

Jon Skeet [C# MVP]

Burns said:
I've just tried the following code:

FileStream objFS = new FileStream(strFileName, FileMode.Open,
FileAccess.Read);
BinaryReader objReader = new BinaryReader(objFS);

UInt32 intFirst = objReader.ReadUInt32();
UInt32 intSecond = 0xC5B8FF00;

Now I know that the first four bytes in the file are C5 B8 FF 00 in
that order, but when I compare intFirst with intSecond they don't
match. On further investigation, intFirst contains 00FFB8C5. WTF?!?!

Why is the Binary Reader reading the bytes in backwards? Is there a
simple way to switch them the right way, or force the Binary Reader
to read them properly?

It's reading them in little-endian order, as documented (admittedly
somewhat poorly).

Now, what wrote the file, and which endianness was it using (assuming
that the first four bytes are indeed meant to be an unsigned integer)?
 
G

Guest

I'm trying to read the Outlook Express DBX files, so I'm not sure which way it was written. Shall I assume that because Microsoft write both .NET and OE, that they both work with endianness

Burns
 
J

Jon Skeet [C# MVP]

Burns said:
I'm trying to read the Outlook Express DBX files, so I'm not sure
which way it was written. Shall I assume that because Microsoft write
both .NET and OE, that they both work with endianness?

It's likely, but you really need to find a detailed specification for
the DBX format. Having had a quick look on wotsit.org, it looks like it
is indeed little-endian.
 
G

Guest

Cheers. The only spec I can find on the DBX format is far from detailed, but I'm trying to figure my way through it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top