473,569 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 27073

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ua******** ******@tk2msftn gp13.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(Binar yReader reader, int fieldSize, ByteOrder
byteOrder)
{
byte[] bytes = new byte[fieldSize];
if (byteOrder == ByteOrder.Littl eEndian)
return reader.ReadByte s(fieldSize);
else
{
for (int i = fieldSize - 1; i > -1; i--)
bytes[i] = reader.ReadByte ();
return bytes;
}
}

public static int ReadInt32(Binar yReader reader, ByteOrder byteOrder)
{
if (byteOrder == ByteOrder.Littl eEndian)
{
return (int)reader.Rea dInt32();
}
else // Big-Endian
{
return BitConverter.To Int32(ReadBytes (reader, 32,
ByteOrder.BigEn dian));
}
}

--
HTH,

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

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ua******** ******@tk2msftn gp13.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"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.co m>
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(reade r, 4, ByteOrder.BigEn dian)...

;)

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:uf******** ******@TK2MSFTN GP15.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(Binar yReader reader, int fieldSize,
ByteOrder byteOrder)
{
byte[] bytes = new byte[fieldSize];
if (byteOrder == ByteOrder.Littl eEndian)
return reader.ReadByte s(fieldSize);
else
{
for (int i = fieldSize - 1; i > -1; i--)
bytes[i] = reader.ReadByte ();
return bytes;
}
}

public static int ReadInt32(Binar yReader reader, ByteOrder byteOrder)
{
if (byteOrder == ByteOrder.Littl eEndian)
{
return (int)reader.Rea dInt32();
}
else // Big-Endian
{
return BitConverter.To Int32(ReadBytes (reader, 32,
ByteOrder.BigEn dian));
}
}

--
HTH,

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

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ua******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP09.phx.gbl...
Works great.
An int32 would be 4 bytes, 32 bites...so you'd do

ReadBytes(reade r, 4, ByteOrder.BigEn dian)...

;)

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:uf******** ******@TK2MSFTN GP15.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(Binar yReader reader, int fieldSize,
ByteOrder byteOrder)
{
byte[] bytes = new byte[fieldSize];
if (byteOrder == ByteOrder.Littl eEndian)
return reader.ReadByte s(fieldSize);
else
{
for (int i = fieldSize - 1; i > -1; i--)
bytes[i] = reader.ReadByte ();
return bytes;
}
}

public static int ReadInt32(Binar yReader reader, ByteOrder byteOrder)
{
if (byteOrder == ByteOrder.Littl eEndian)
{
return (int)reader.Rea dInt32();
}
else // Big-Endian
{
return BitConverter.To Int32(ReadBytes (reader, 32,
ByteOrder.BigEn dian));
}
}

--
HTH,

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

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ua******** ******@tk2msftn gp13.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
5119
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 starting at position 0. I thought that char was 2 bytes, but this makes it seem as though it is just 1 when I write to a file. Why is this? I imagine...
6
2984
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. The code calls the bw.write(TheValue), but nothing is written after 1kb. I'm I missing something? Here's the way I create my FileStream and...
8
27445
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
5749
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 did some simple testing and it seems that the string data is prefixed w/a variable number of bytes that indicate the length. 1 byte if length <=...
4
2812
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 = objReader.ReadUInt32() UInt32 intSecond = 0xC5B8FF00
17
3788
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 object garbage collection after the WriteSomething method is executed? ---- using System; using System.IO;
1
6224
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 object where I plan to create a BinaryReader to extract the data and format it ready for a database insert. At the moment I close the binarywriter...
1
2317
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 WriteUnicodeStringToStream (Stream s, string string) { BinaryWriter bw = new BinaryWriter (s, Encoding.Unicode); bw.Write (string); }
3
1793
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 like so: Public Overloads Overrides Sub Write(ByVal Value As Byte) m_Queue.Enqueue(New WriteRequest(MyBase.BaseStream.Position, Value)) End Sub...
0
7711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7628
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7938
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7694
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7993
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6317
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5245
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3679
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

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.