473,787 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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 5971
On 2004-10-07, PHil Coveney <PH*********@di scussions.micro soft.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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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.IPAd dress.HostToNet workOrder to convert
the integer first....

using System;
using System.Net;

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

Console.WriteLi ne (BitConverter.T oString (BitConverter.G etBytes (i)));
Console.WriteLi ne (BitConverter.T oString (BitConverter.G etBytes (j)));
}
}

--
Tom Shelton [MVP]
Nov 22 '05 #2
On 2004-10-07, PHil Coveney <PH*********@di scussions.micro soft.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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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.IPAd dress.HostToNet workOrder to convert
the integer first....

using System;
using System.Net;

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

Console.WriteLi ne (BitConverter.T oString (BitConverter.G etBytes (i)));
Console.WriteLi ne (BitConverter.T oString (BitConverter.G etBytes (j)));
}
}

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

public void WriteInt32BigEn dian ( 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*@YOUKNOWTHE DRILLmtogden.co m> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
On 2004-10-07, PHil Coveney <PH*********@di scussions.micro soft.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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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.IPAd dress.HostToNet workOrder to convert
the integer first....

using System;
using System.Net;

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

Console.WriteLi ne (BitConverter.T oString (BitConverter.G etBytes (i)));
Console.WriteLi ne (BitConverter.T oString (BitConverter.G etBytes (j)));
}
}

--
Tom Shelton [MVP]

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

public void WriteInt32BigEn dian ( 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*@YOUKNOWTHE DRILLmtogden.co m> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
On 2004-10-07, PHil Coveney <PH*********@di scussions.micro soft.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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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.IPAd dress.HostToNet workOrder to convert
the integer first....

using System;
using System.Net;

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

Console.WriteLi ne (BitConverter.T oString (BitConverter.G etBytes (i)));
Console.WriteLi ne (BitConverter.T oString (BitConverter.G etBytes (j)));
}
}

--
Tom Shelton [MVP]

Nov 22 '05 #5
PHil Coveney <PH*********@di scussions.micro soft.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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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 EndianBinaryWri ter which allows you to specify
endianness:

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

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6
PHil Coveney <PH*********@di scussions.micro soft.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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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 EndianBinaryWri ter which allows you to specify
endianness:

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

--
Jon Skeet - <sk***@pobox.co m>
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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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.Enc oding.BigEndian Unicode encoding
method.

--
Chris

dunawayc[AT]sbcglobal_lunch meat_[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*********@di scussions.micro soft.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(aF ileStream, Encoding.ASCII) ;
int someValue = 1;
aWriter.WriteIn t32(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 EndianBinaryWri ter which allows you to specify
endianness:

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

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

Nov 22 '05 #9
PHil Coveney <PH*********@di scussions.micro soft.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.co m>
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
1306
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 = 1; aWriter.WriteInt32(someValue); the byte stream that appears in the file is 01 00 00 00
12
7318
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 little-endian machine, the byte order is little-endian. But what if I run C# on a big- endian machine, say, using Mono? Does the C# language (or IL) make any guarantees as to the byte order?
17
1413
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 for many different food items found ni the html including pizza, burgers, fries etc. what i want is to write out to a text file in the following order: pizza, pizza hut, 3.00
20
19801
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
5343
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 greatest number, smallest number, and the average
0
10363
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.