473,394 Members | 1,956 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.

writing ints to file in reverse byte order

Hello,

I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?

Thanks in advance,

--
PC
Nov 22 '05 #1
9 5939
On 2004-10-07, PHil Coveney <PH*********@discussions.microsoft.com> wrote:
Hello,

I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?

Thanks in advance,


You could try using System.Net.IPAddress.HostToNetworkOrder to convert
the integer first....

using System;
using System.Net;

public class test
{
public static void Main ()
{
int i = 1;
int j = IPAddress.HostToNetworkOrder (i);

Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (i)));
Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (j)));
}
}

--
Tom Shelton [MVP]
Nov 22 '05 #2
On 2004-10-07, PHil Coveney <PH*********@discussions.microsoft.com> wrote:
Hello,

I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?

Thanks in advance,


You could try using System.Net.IPAddress.HostToNetworkOrder to convert
the integer first....

using System;
using System.Net;

public class test
{
public static void Main ()
{
int i = 1;
int j = IPAddress.HostToNetworkOrder (i);

Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (i)));
Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (j)));
}
}

--
Tom Shelton [MVP]
Nov 22 '05 #3
You can also do by yourself with some bit shifting:

public void WriteInt32BigEndian ( int value )
{
base.Write( new byte [0x4] {
(byte) (value >> 0x18),
(byte) (value >> 0x10),
(byte) (value >> 0x8),
(byte) (value)
} );
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
On 2004-10-07, PHil Coveney <PH*********@discussions.microsoft.com> wrote:
Hello,

I need to support a file format that stores integers with the MSB
first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this.
Does
anyone have a recommendation?

Thanks in advance,


You could try using System.Net.IPAddress.HostToNetworkOrder to convert
the integer first....

using System;
using System.Net;

public class test
{
public static void Main ()
{
int i = 1;
int j = IPAddress.HostToNetworkOrder (i);

Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (i)));
Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (j)));
}
}

--
Tom Shelton [MVP]

Nov 22 '05 #4
You can also do by yourself with some bit shifting:

public void WriteInt32BigEndian ( int value )
{
base.Write( new byte [0x4] {
(byte) (value >> 0x18),
(byte) (value >> 0x10),
(byte) (value >> 0x8),
(byte) (value)
} );
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
On 2004-10-07, PHil Coveney <PH*********@discussions.microsoft.com> wrote:
Hello,

I need to support a file format that stores integers with the MSB
first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this.
Does
anyone have a recommendation?

Thanks in advance,


You could try using System.Net.IPAddress.HostToNetworkOrder to convert
the integer first....

using System;
using System.Net;

public class test
{
public static void Main ()
{
int i = 1;
int j = IPAddress.HostToNetworkOrder (i);

Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (i)));
Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (j)));
}
}

--
Tom Shelton [MVP]

Nov 22 '05 #5
PHil Coveney <PH*********@discussions.microsoft.com> wrote:
I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?


You could use my EndianBinaryWriter which allows you to specify
endianness:

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6
PHil Coveney <PH*********@discussions.microsoft.com> wrote:
I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?


You could use my EndianBinaryWriter which allows you to specify
endianness:

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #7
On Wed, 6 Oct 2004 23:43:01 -0700, PHil Coveney wrote:
Hello,

I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?

Thanks in advance,


In addition, check out the system.Text.Encoding.BigEndianUnicode encoding
method.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 22 '05 #8
Hi Jon,

Wow, I got several very helpful responses to this post...but yours, the
one that said "use this, it's already done," wins the prize. Thanks for your
help, and your DLL.

PC

"Jon Skeet [C# MVP]" wrote:
PHil Coveney <PH*********@discussions.microsoft.com> wrote:
I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?


You could use my EndianBinaryWriter which allows you to specify
endianness:

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

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

Nov 22 '05 #9
PHil Coveney <PH*********@discussions.microsoft.com> wrote:
Wow, I got several very helpful responses to this post...but yours, the
one that said "use this, it's already done," wins the prize. Thanks for your
help, and your DLL.


My pleasure - just let me know if you have any problems with it :)

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

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

Similar topics

6
by: PHil Coveney | last post by:
Hello, I need to support a file format that stores integers with the MSB first and the LSB last. When I use BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII); int someValue...
12
by: Michi Henning | last post by:
Looking at the language spec, I can't find a statement about the byte order for value types, such as int, float, etc. Are they guaranteed to be little-endian or big- endian? I know that, on a...
17
by: homepricemaps | last post by:
hey folks, have a logic question for you. appreciate the help in advance. i am scraping 3 pieces of information from the html namely the food name , store name and price. and i am doing this...
20
by: mike7411 | last post by:
Is there any easy way to reverse the order of the bits in a byte in C++? (i.e. 00000001 becomes 10000000)
6
by: aburningflame23 | last post by:
alright this is my problem.....please help i need to write a program that reads 10 integers from a file into an array and then prints out the numbers in reverse order. It also prints out the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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.