473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

BinaryReader -- can it be forced to read a BigEndian stream

Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory
stream or even file stream in big endian order or do I have to write
something custom to reverse the byte order? So, for example, I'd like a
Windows client PC (native little endian) to be able to read a network stream
coming in as big endian. I want to be able to read a short for example
without worrying about flipping the bytes.

Thank you,
Laszlo
Nov 16 '05 #1
14 18985
Hi Laszlo,

The BinaryReader uses UTF-8 encoding by default. You can specify your own encoding by using the overloaded constructor.

BinaryReader br = new BinaryReader(binaryStream, System.Text.Encoding.BigEndianUnicode);

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Thanks again. When I set the Encoding, does that work for non-text data
types. So, when I do a ReadUInt16, does it read them BigEndian? I'll give
it a try. Thank you.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr8picjpgklbvpo@morten_x.edunord...
Hi Laszlo,

The BinaryReader uses UTF-8 encoding by default. You can specify your own encoding by using the overloaded constructor.
BinaryReader br = new BinaryReader(binaryStream, System.Text.Encoding.BigEndianUnicode);
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3
Morten Wennevik <Mo************@hotmail.com> wrote:
The BinaryReader uses UTF-8 encoding by default. You can specify your
own encoding by using the overloaded constructor.

BinaryReader br = new BinaryReader(binaryStream,
System.Text.Encoding.BigEndianUnicode);


I believe the OP is talking about the various other calls to
BinaryReader: ReadInt16 etc, not reading strings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory
stream or even file stream in big endian order or do I have to write
something custom to reverse the byte order? So, for example, I'd like a
Windows client PC (native little endian) to be able to read a network stream
coming in as big endian. I want to be able to read a short for example
without worrying about flipping the bytes.


I don't believe there is any way of doing it with the current
BinaryReader. I've considered a few times writing a BitConverter and
BinaryReader where you can specify the endianness, but never got round
to it. Let me know if you'd particularly like it and I'll see what I
can do - no promises of delivery date though!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Thank you, John,

I really appreciate it. I've actually started writing my own
BinaryReaderBE. I can read basic data types. I've also extended it so that
it can read arrays of more than just bytes (so, short[], ushort[], int[],
uint[]). And, finally, I've added a function which would allow you to read
a struct (or class) out of a binary stream. You pass it a System.Type
object and it uses reflection to populate the various members, returning an
object. You have to cast the returned object to whatever type you want, but
it works really well. I've also implemented this for the Little Endian
Binary Reader in that same class. Pretty simple to do, and, given that
Network Byte Order teds to be Big Endian and that we here at NASA use Big
Endian for most things (we use a lot of Motorola CPUs), I'm surprised that
Microsoft doesn't have a Big Endian binary reader.

Inside the BinaryReaderBE, I have a private method which takes a byte[] and
flips it. Inside the ReadInt16, method, then, it returns a short
reconstructed from the reversed byte[] throught the BitConverter.

Thanks very much for the offer of help,
Laszlo

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory stream or even file stream in big endian order or do I have to write
something custom to reverse the byte order? So, for example, I'd like a
Windows client PC (native little endian) to be able to read a network stream coming in as big endian. I want to be able to read a short for example
without worrying about flipping the bytes.


I don't believe there is any way of doing it with the current
BinaryReader. I've considered a few times writing a BitConverter and
BinaryReader where you can specify the endianness, but never got round
to it. Let me know if you'd particularly like it and I'll see what I
can do - no promises of delivery date though!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
I really appreciate it. I've actually started writing my own
BinaryReaderBE. I can read basic data types. I've also extended it so that
it can read arrays of more than just bytes (so, short[], ushort[], int[],
uint[]). And, finally, I've added a function which would allow you to read
a struct (or class) out of a binary stream. You pass it a System.Type
object and it uses reflection to populate the various members, returning an
object.
How does it know what order to populate the members in? The order it
comes back from the Type.GetProperties call (or whatever) isn't
specified, so you should be a bit wary.
You have to cast the returned object to whatever type you want, but
it works really well. I've also implemented this for the Little Endian
Binary Reader in that same class. Pretty simple to do, and, given that
Network Byte Order teds to be Big Endian and that we here at NASA use Big
Endian for most things (we use a lot of Motorola CPUs), I'm surprised that
Microsoft doesn't have a Big Endian binary reader.
Yes, it is a bit short-sighted.
Inside the BinaryReaderBE, I have a private method which takes a byte[] and
flips it. Inside the ReadInt16, method, then, it returns a short
reconstructed from the reversed byte[] throught the BitConverter.


Right. I thought of using that, but reckoned it would probably be
easier (and faster) to do the conversions myself within my own
BinaryReader. I'll let you know if I find time to work on my
EndianBitConverter and EndianBinaryReader/Writer classes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
I really appreciate it. I've actually started writing my own
BinaryReaderBE. I can read basic data types. I've also extended it so that it can read arrays of more than just bytes (so, short[], ushort[], int[], uint[]). And, finally, I've added a function which would allow you to read a struct (or class) out of a binary stream. You pass it a System.Type
object and it uses reflection to populate the various members, returning an object.


How does it know what order to populate the members in? The order it
comes back from the Type.GetProperties call (or whatever) isn't
specified, so you should be a bit wary.


I use reflection as in the code snippet below. I've used this approach
before. It seems to work well.

public object ReadStruct(Type type)
{
object objectStruct = Activator.CreateInstance(type);
foreach (FieldInfo fieldInfo in type.GetFields())
{
switch(fieldInfo.FieldType.ToString())
{
case "System.Byte":
fieldInfo.SetValue(objectStruct, ReadByte());
break;
... [rest of supported data types]
}
}

return objectStruct;
}

You would call it as follows.

BinaryReaderBE binaryReaderBE = new BinaryReaderBE([stream object]);
[ClassName] class =
([ClassName])binaryReaderBE.ReadStruct(typeof([ClassName]);

Nov 16 '05 #8
I've never had any trouble with stuff being out of order except if I've
changed my struct mid stream. Then, for some reason, I have to delete all
the stuff in the "bin" directories and THEN recompile. Otherwise, the order
always seems to come back as listed in the Class (or struct) Definition.
Nov 16 '05 #9
Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
I've never had any trouble with stuff being out of order except if I've
changed my struct mid stream. Then, for some reason, I have to delete all
the stuff in the "bin" directories and THEN recompile. Otherwise, the order
always seems to come back as listed in the Class (or struct) Definition.


Yes, it does *now* - but it's not guaranteed that it will in the
future. You're relying on the implementation of GetFields not changing,
and I suspect it's very likely that it will in the future.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
> Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
I've never had any trouble with stuff being out of order except if I've
changed my struct mid stream. Then, for some reason, I have to delete all the stuff in the "bin" directories and THEN recompile. Otherwise, the order always seems to come back as listed in the Class (or struct) Definition.


Yes, it does *now* - but it's not guaranteed that it will in the
future. You're relying on the implementation of GetFields not changing,
and I suspect it's very likely that it will in the future.

--
Jon Skeet - <sk***@pobox.com>


Could [StructLayout(LayoutKind.Sequential)] help with this?
Nov 16 '05 #11
Hmmm. OK. Then how else would I do something like this. It really would
be helpful if I could read a struct / class out of a stream (like you can in
C++) without resorting to unsafe code and pointers.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
I've never had any trouble with stuff being out of order except if I've
changed my struct mid stream. Then, for some reason, I have to delete all the stuff in the "bin" directories and THEN recompile. Otherwise, the order always seems to come back as listed in the Class (or struct) Definition.


Yes, it does *now* - but it's not guaranteed that it will in the
future. You're relying on the implementation of GetFields not changing,
and I suspect it's very likely that it will in the future.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #12
If that works, is there an equivalent for a class? Unfortunately, with a
struct, if you have an array of any kind, you can't specify how many in that
array, so my algorithm won't work, whereas with a class, you can go

public short[] shrt = new short[5];

"Filip Strugar" <fili@sezampro_dot_yu> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
I've never had any trouble with stuff being out of order except if I've changed my struct mid stream. Then, for some reason, I have to delete all the stuff in the "bin" directories and THEN recompile. Otherwise, the order always seems to come back as listed in the Class (or struct)
Definition.
Yes, it does *now* - but it's not guaranteed that it will in the
future. You're relying on the implementation of GetFields not changing,
and I suspect it's very likely that it will in the future.

--
Jon Skeet - <sk***@pobox.com>


Could [StructLayout(LayoutKind.Sequential)] help with this?

Nov 16 '05 #13
Filip Strugar <fili@sezampro_dot_yu> wrote:
Could [StructLayout(LayoutKind.Sequential)] help with this?


It wouldn't guarantee the order fields are returned in, no. It
determines the order of the fields in memory, but that's different.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14
Laszlo Szijarto <ls*******@nospam.mrdoc.cc> wrote:
Hmmm. OK. Then how else would I do something like this. It really would
be helpful if I could read a struct / class out of a stream (like you can in
C++) without resorting to unsafe code and pointers.


Well, you could always sort the fields yourself, eg alphabetically.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rafal 'Raf256' Maj | last post by:
Hi, I need to parse a file. This means reading from it as from std::istream. But - sometimes I also need to put-back some text I read before. What type of string can I use for that? Something...
1
by: ywchan | last post by:
I have used a stream reader and sometimes it contains more than one rows of text. How to read all the lines in a stream reader? sr.Readline can only get line by line...and every times the total...
2
by: om | last post by:
hi I an new to c++ and was wondering if anyone could advise me how to read a stream coming from a http source thanks alot...
2
by: James Minns | last post by:
Hi, I have the following problem with my VB code: accented characters are being transformed into a cr-lf pair! I am reading a sequence of bytes from a binary file, one part of which is a text...
4
by: csharpula csharp | last post by:
Hello,I am trying to read from a zip stream and one of the files is of a 0 size (0 bytes)- Exception is thrown as a result but there are some other files after it in a stream which I want to keep...
6
by: =?Utf-8?B?RGFu?= | last post by:
I need to read an HTTP stream that is defined as excel format. How can I access this via ADO.Net? This dataset pastes nicely into excel....
2
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
11
by: csharpula csharp | last post by:
Hello, I would like to know how can I read a file by this way: Read from line X to Y and in other iteration from line Y to P and on and on. How to implement it? Which method and how? Thank u! ...
3
by: hdbbdh | last post by:
Hello everyone, I try to read a stream and save it as byte array to add it later to zip file. this is the code that I use Dim totalbytes As Byte() = Nothing Dim iBytesRead As Integer Dim...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.