473,394 Members | 1,752 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,394 software developers and data experts.

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 27023

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
6
by: ThunderMusic | last post by:
Hi, In my app, I open a file using a FileStream then pass it to a BinaryWriter. I then use the BinaryWriter instance to write to my file. But a problem arose : The file never gets bigger than 1kb....
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
6
by: John Aldrin | last post by:
Hi, I'm looking for info that explains the format of a string data type when written to a stream using a BinaryWriter. I've looked all over MSDN and Internet and I cannot seem to find it. I...
4
by: Burns | last post by:
Hi I've just tried the following code FileStream objFS = new FileStream(strFileName, FileMode.Open, FileAccess.Read) BinaryReader objReader = new BinaryReader(objFS) UInt32 intFirst =...
17
by: Filip Strugar | last post by:
Considering that the BinaryWriter/BinaryReader object closes the underlaying stream upon being gc collected, is the following code correct, and if it is what is the reason preventing BinaryWriter...
1
by: Claire | last post by:
I'm writing from a serial port into a MemoryStream via a BinaryWriter in my protocol object. After I've filled the memorystream, which is approx 200KB in size, I then pass it up to my datahandler...
1
by: Barguast | last post by:
Is it necessary to call the close method for every BinaryWriter that I create? Or is it just a way to close the underlying stream? For example is it OK to do the following: private void...
3
by: Eugene | last post by:
I'm trying to write a class which uses BinaryWriter as its base but allows for queuing of write requests Public Class QueuedBinaryWriter Inherits BinaryWriter I override all the Write methods...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.