How to write ints to a text file with low bits on the right side (Bigendian)

By default, the BinaryWriter class writes int values ​​with the least significant digits to the left (for example, (int) 6 becomes 06 00 00 00 when the resulting file is viewed in a hex editor). I need low digits on the right (e.g. 00 00 00 06).

How do I achieve this?

EDIT: Thank you, guard, for giving me the name of what I was looking for. I edited the title and tags to make them easier to find.

+3
source share
6 answers

Jon Skeet EndianBitConverter , . / , . , ; -p

        int i = 6;
        byte[] raw = new byte[4] {
            (byte)(i >> 24), (byte)(i >> 16),
            (byte)(i >> 8), (byte)(i)};
+6

, : EndianBit *. : P

+6

" " ( "Endianness" ). endian. , #, , BinaryWritter.

System.BitConverter.IsLittleEndian. , BinaryWriter endian .

+2

- , System.Net.IPAddress.HostToNetworkOrder().

, , , "" , "" , , .

, , .

+1

, , . - :

Byte[] bytes = BitConverter.GetBytes(number);
Array.Reverse(bytes);

// Then, to write the values you use
writer.Write(bytes);
+1

, connect ticket BinaryReder/Writer Bigendian . .

0

All Articles