469,288 Members | 2,357 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

BinaryWriter/Reader big endian

The BinaryWriter/Reader's are little-endian only. Was hoping they were going
to be made more flexible in 2.0, but that doesn't appear to be the case.

Anyone already done all the work of implementing this? :)

Karl
--
http://www.openmymind.net/
Feb 8 '06 #1
6 25325

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ua**************@tk2msftngp13.phx.gbl...
The BinaryWriter/Reader's are little-endian only. Was hoping they were
going to be made more flexible in 2.0, but that doesn't appear to be the
case.

Anyone already done all the work of implementing this? :)
I was hoping so as well.
In addition, any word of a .Net binary stream or serialzation library
compat. with a Java serializaion or Java DataInput/Output stream would
great. This on the .Net server side would save me quite a bit of mobile
badwidth and parsing processing power on the J2ME client side vis-a-vis XML.

Karl
--
http://www.openmymind.net/

Feb 8 '06 #2
Here's an adapted version of one I've used successfully for reading TIFF
files. Note that it requires a "fieldsize" parameter. This is because the
stream is read the same direction regardless of the byte order. It is the
size in bytes of the data that affects the reading of the bytes from the
stream. It returns the bytes in the correct order after reading, as an array
of bytes. It can be used with other methods to get specific types, such as
Int32, Int64, etc. For example, if you know you're reading Int32 data, you
would pass 32 to the fieldSize parameter. Then you use the BitConverter
class to convert the byte array to whatever data type it is. Below the enum
and method definition I have an example for Int32:

public enum ByteOrder : int
{
LittleEndian,
BigEndian
}

public static byte[] ReadBytes(BinaryReader reader, int fieldSize, ByteOrder
byteOrder)
{
byte[] bytes = new byte[fieldSize];
if (byteOrder == ByteOrder.LittleEndian)
return reader.ReadBytes(fieldSize);
else
{
for (int i = fieldSize - 1; i > -1; i--)
bytes[i] = reader.ReadByte();
return bytes;
}
}

public static int ReadInt32(BinaryReader reader, ByteOrder byteOrder)
{
if (byteOrder == ByteOrder.LittleEndian)
{
return (int)reader.ReadInt32();
}
else // Big-Endian
{
return BitConverter.ToInt32(ReadBytes(reader, 32,
ByteOrder.BigEndian));
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.
"Mike" <vi********@yahoo.com> wrote in message
news:Of*************@TK2MSFTNGP14.phx.gbl...

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ua**************@tk2msftngp13.phx.gbl...
The BinaryWriter/Reader's are little-endian only. Was hoping they were
going to be made more flexible in 2.0, but that doesn't appear to be the
case.

Anyone already done all the work of implementing this? :)


I was hoping so as well.
In addition, any word of a .Net binary stream or serialzation library
compat. with a Java serializaion or Java DataInput/Output stream would
great. This on the .Net server side would save me quite a bit of mobile
badwidth and parsing processing power on the J2ME client side vis-a-vis
XML.

Karl
--
http://www.openmymind.net/


Feb 8 '06 #3
<"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO .
ANDME net>> wrote:
The BinaryWriter/Reader's are little-endian only. Was hoping they were going
to be made more flexible in 2.0, but that doesn't appear to be the case.

Anyone already done all the work of implementing this? :)


Yup:

http://www.pobox.com/~skeet/csharp/miscutil

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 8 '06 #4
Great, I'll take a look at it :)

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO .
ANDME net>> wrote:
The BinaryWriter/Reader's are little-endian only. Was hoping they were
going
to be made more flexible in 2.0, but that doesn't appear to be the case.

Anyone already done all the work of implementing this? :)


Yup:

http://www.pobox.com/~skeet/csharp/miscutil

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

Feb 9 '06 #5
Works great.
An int32 would be 4 bytes, 32 bites...so you'd do

ReadBytes(reader, 4, ByteOrder.BigEndian)...

;)

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uf**************@TK2MSFTNGP15.phx.gbl...
Here's an adapted version of one I've used successfully for reading TIFF
files. Note that it requires a "fieldsize" parameter. This is because the
stream is read the same direction regardless of the byte order. It is the
size in bytes of the data that affects the reading of the bytes from the
stream. It returns the bytes in the correct order after reading, as an
array of bytes. It can be used with other methods to get specific types,
such as Int32, Int64, etc. For example, if you know you're reading Int32
data, you would pass 32 to the fieldSize parameter. Then you use the
BitConverter class to convert the byte array to whatever data type it is.
Below the enum and method definition I have an example for Int32:

public enum ByteOrder : int
{
LittleEndian,
BigEndian
}

public static byte[] ReadBytes(BinaryReader reader, int fieldSize,
ByteOrder byteOrder)
{
byte[] bytes = new byte[fieldSize];
if (byteOrder == ByteOrder.LittleEndian)
return reader.ReadBytes(fieldSize);
else
{
for (int i = fieldSize - 1; i > -1; i--)
bytes[i] = reader.ReadByte();
return bytes;
}
}

public static int ReadInt32(BinaryReader reader, ByteOrder byteOrder)
{
if (byteOrder == ByteOrder.LittleEndian)
{
return (int)reader.ReadInt32();
}
else // Big-Endian
{
return BitConverter.ToInt32(ReadBytes(reader, 32,
ByteOrder.BigEndian));
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
We got a sick zebra a hat,
you ultimate tuna.
"Mike" <vi********@yahoo.com> wrote in message
news:Of*************@TK2MSFTNGP14.phx.gbl...

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ua**************@tk2msftngp13.phx.gbl...
The BinaryWriter/Reader's are little-endian only. Was hoping they were
going to be made more flexible in 2.0, but that doesn't appear to be the
case.

Anyone already done all the work of implementing this? :)


I was hoping so as well.
In addition, any word of a .Net binary stream or serialzation library
compat. with a Java serializaion or Java DataInput/Output stream would
great. This on the .Net server side would save me quite a bit of mobile
badwidth and parsing processing power on the J2ME client side vis-a-vis
XML.

Karl
--
http://www.openmymind.net/



Feb 9 '06 #6
Oops! Sorry, Karl. As I said, it was adopted from some code that worked, and
I was in a hurry when writing the derived ReadInt32 method. The ReadBytes
method I simply copied, and removed the remarks. In my original class, I
only used the ReadBytes method for things like reading pixels, and used the
actual code in it for the specific data type methods, in order to avoid a
bit of indirection.

I hate it when I do something stupid like that! Apologies.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:uR**************@TK2MSFTNGP09.phx.gbl...
Works great.
An int32 would be 4 bytes, 32 bites...so you'd do

ReadBytes(reader, 4, ByteOrder.BigEndian)...

;)

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uf**************@TK2MSFTNGP15.phx.gbl...
Here's an adapted version of one I've used successfully for reading TIFF
files. Note that it requires a "fieldsize" parameter. This is because the
stream is read the same direction regardless of the byte order. It is the
size in bytes of the data that affects the reading of the bytes from the
stream. It returns the bytes in the correct order after reading, as an
array of bytes. It can be used with other methods to get specific types,
such as Int32, Int64, etc. For example, if you know you're reading Int32
data, you would pass 32 to the fieldSize parameter. Then you use the
BitConverter class to convert the byte array to whatever data type it is.
Below the enum and method definition I have an example for Int32:

public enum ByteOrder : int
{
LittleEndian,
BigEndian
}

public static byte[] ReadBytes(BinaryReader reader, int fieldSize,
ByteOrder byteOrder)
{
byte[] bytes = new byte[fieldSize];
if (byteOrder == ByteOrder.LittleEndian)
return reader.ReadBytes(fieldSize);
else
{
for (int i = fieldSize - 1; i > -1; i--)
bytes[i] = reader.ReadByte();
return bytes;
}
}

public static int ReadInt32(BinaryReader reader, ByteOrder byteOrder)
{
if (byteOrder == ByteOrder.LittleEndian)
{
return (int)reader.ReadInt32();
}
else // Big-Endian
{
return BitConverter.ToInt32(ReadBytes(reader, 32,
ByteOrder.BigEndian));
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
We got a sick zebra a hat,
you ultimate tuna.
"Mike" <vi********@yahoo.com> wrote in message
news:Of*************@TK2MSFTNGP14.phx.gbl...

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ua**************@tk2msftngp13.phx.gbl...
The BinaryWriter/Reader's are little-endian only. Was hoping they were
going to be made more flexible in 2.0, but that doesn't appear to be
the case.

Anyone already done all the work of implementing this? :)

I was hoping so as well.
In addition, any word of a .Net binary stream or serialzation library
compat. with a Java serializaion or Java DataInput/Output stream would
great. This on the .Net server side would save me quite a bit of mobile
badwidth and parsing processing power on the J2ME client side vis-a-vis
XML.
Karl
--
http://www.openmymind.net/



Feb 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by ThunderMusic | last post: by
8 posts views Thread by Perception | last post: by
6 posts views Thread by John Aldrin | last post: by
4 posts views Thread by Burns | last post: by
17 posts views Thread by Filip Strugar | last post: by
1 post views Thread by Claire | last post: by
1 post views Thread by Barguast | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.