C# Equivalent of Python struct.pack

SilverShaded

Well-known member
Joined
Mar 7, 2020
Messages
110
Programming Experience
10+
Guys, im trying to convert some python code to C#

In python there is this command and the result is 'x10E'
self.v5_controlcode = struct.pack("<H", 0x4510)

<H implies unsigned integer,

I cant for the life of me get 0x4510 to convert to x10E?

For many other values I can reproduce the results but not for this one :( anyone know how to do the conversion?
 
<H implies unsigned integer,
I don't know Python, just did some research.
In format <H the < means little-endian, and H unsigned two-byte integer.
0x values means hex string and 0x4510 is the decimal value 17680.

0x notation can be used in C# too, and you can convert it to ushort/uint16.
Then there is BitConverter where you can get the bytes values and also check endianess.
C#:
var value =(ushort) 0x4510;
var endian = BitConverter.IsLittleEndian;
var bytes = BitConverter.GetBytes(value); // byte values 16 69
If you convert each byte to hex you come back to 10 45.

I haven't quite figured out how it becomes x10E, Python prints b'\x10E' where b means binary and \x escape sequence.
10 appears as hex value, while E is the ascii char for decimal 69 / hex 45. Possibly it is using hex for unprintable chars.
 
"<H" is actually means little endian unsigned short integer, not unsigned integer. Shorts are only 2 bytes, while modern integers are 4 bytes.

Anyway, the result is not actually "x10E". The result is that you get a two byte array with these values:
C#:
[ 0x10, 0x45 ]

The byte 0x10 is the non-printing ASCII character for DLE (Data Link Escape), and most code will just display it as 0x10 since it is non printing.
The byte 0x45 corresponds to the ASCII character 'E'.
 
I don't know Python, just did some research.
In format <H the < means little-endian, and H unsigned two-byte integer.
0x values means hex string and 0x4510 is the decimal value 17680.

0x notation can be used in C# too, and you can convert it to ushort/uint16.
Then there is BitConverter where you can get the bytes values and also check endianess.
C#:
var value =(ushort) 0x4510;
var endian = BitConverter.IsLittleEndian;
var bytes = BitConverter.GetBytes(value); // byte values 16 69
If you convert each byte to hex you come back to 10 45.

I haven't quite figured out how it becomes x10E, Python prints b'\x10E' where b means binary and \x escape sequence.
10 appears as hex value, while E is the ascii char for decimal 69 / hex 45. Possibly it is using hex for unprintable chars.

I've spent most of the day on this with not much progress, it turns out that 0x10E is just the displayed value e.g. in debug, if you list the array out properly its acually a two byte array with more menaingfull numbers (16, 69), why they decide to display that as x10E is a mystery to me which is one reason I dislike python... far easier to see the actual numbers stored.
 
I suspected the b indicated rest was just a string representation of the binary data, a bit weird to mix hex and ascii.
 
I suspected the b indicated rest was just a string representation of the binary data, a bit weird to mix hex and ascii.

It was really confusing me for a while, the displayed array length was not the actual array length which is important in building the modbus frame, actually a modified modbus frame for a wifi data logger. Never going to work if its wrong format. Displaying it combined with ascii codes is just annoying... :ROFLMAO:
 
Back
Top Bottom