Method difference between languages ​​(Python-> C #)

I am trying to reproduce a sequence of code from a Python program in C #. In Python, I have:

element1, element2 = struct.unpack('!hh', data[2:6])

The above statement is unpacked from the "substring" of data in short short format (network byte order). The given values ​​(element1, element2): 96 and 16

My attempt in C #:

byte[] bytesOfInterval = ASCIIEncoding.ASCII.GetBytes (data.Substring (2, 4));
using (MemoryStream stream = new MemoryStream(bytesOfInterval)) {
    using (BinaryReader reader = new BinaryReader(stream)) {
        Logger.Trace (reader.ReadInt16().ToString());
        Logger.Trace (reader.ReadInt16().ToString());
    }
}

It outputs: 24576 and 4096 .

As you can see, exiting the Python program is slightly different from C #. To check the "substrings" (input), I encoded them in hexadecimal format to find out if there is a difference. They were equal to 00600010 , so the input is the same, the output is different. Why?

Notes:

+4
1

, endianness , ,

Int16 x1 = 4096;  
var x2 = IPAddress.HostToNetworkOrder(x1);

x2 16 ( 24576 = > 96)

, IPAddress.HostToNetworkOrder.

+2

All Articles