ctypes.c_uint16 is the ctypes type for a 16-bit unsigned integer (C uint16_t), with a range of 0 to 65,535. You use it to describe ports, sensor readings, protocol fields and any C function parameter or struct member that is two bytes wide. It is the same class as ctypes.c_ushort.
Unlike a Python int, a c_uint16 has a fixed size and a fixed byte layout, which is what C code expects.
c_uint16 stores a value in exactly two bytes. The value is available through .value, and ctypes.sizeof reports the size.
The alias exists because ctypes maps the fixed-width names to whichever C type has the right size. The C unsigned short is 16 bits on every mainstream platform.
Assigning a value outside 0 to 65,535 does not raise an exception. ctypes keeps the low 16 bits.
This mirrors C unsigned arithmetic and is useful for emulating counters that roll over. It is a bug when it happens unexpectedly, so validate ranges on untrusted input before you store it.
bytes(value) shows the raw bytes in the machine's native order. On x86-64 and ARM in their usual little-endian mode the low byte comes first.
Network protocols and many file formats use big-endian ("network order"). Do not rely on the native layout when you exchange data. Either convert explicitly with to_bytes and from_bytes, or declare a BigEndianStructure (see below).
Declare the signature so ctypes passes and returns a 16-bit value. The C function htons converts host order to network order.
Without argtypes and restype, ctypes assumes int for both, which truncates or mis-reads 16-bit results on some platforms. Always declare them.
Use c_uint16 as a structure field to mirror a C struct. A BigEndianStructure with _pack_ = 1 matches a typical packed wire format.
On Python 3.14 and later, setting _pack_ without _layout_ = "ms" emits a DeprecationWarning, so add _layout_ = "ms" there (it is accepted on every platform). Without packing the compiler-style padding would make the structure larger than the 7 bytes on the wire. Arrays work the same way as for other types:
| Type | Size | Range |
|---|---|---|
c_uint8 | 1 byte | 0 to 255 |
c_uint16 | 2 bytes | 0 to 65,535 |
c_int16 | 2 bytes | -32,768 to 32,767 |
c_uint32 | 4 bytes | 0 to 4,294,967,295 |
Pick the width and signedness that match the C declaration. A mismatch corrupts values without any error message.
On mainstream platforms none: they are the same class. Use c_uint16 when the C header says uint16_t and you want the width to be obvious.
No. It silently keeps the low 16 bits, so c_uint16(65536).value is 0.
Use int.from_bytes(data, "big"), or a BigEndianStructure with a c_uint16 field. The struct module with the ">H" format does the same.
Yes, for binary parsing and fixed-width arithmetic. For pure Python parsing, struct is usually simpler.
ctypes.c_uint16 is a two-byte unsigned integer matching C uint16_t. It wraps on overflow, uses native byte order unless you choose a big-endian structure, and needs explicit argtypes and restype for foreign calls. Use it for ports, protocol fields and packed structures.