The ctypes.c_byte type represents the C signed char, a single byte holding a whole number from -128 to 127. You use it to pass small signed integers to C functions, to declare byte-sized structure fields, and to build arrays of raw bytes. It is a type in the ctypes module, not a module itself.
Because it is signed, values outside the range wrap around silently instead of raising an error. For unsigned bytes from 0 to 255, use c_ubyte.
c_byte is a ctypes simple type that occupies one byte. You read and write it through .value:
The range is fixed by the byte width:
| Type | C equivalent | Range |
|---|---|---|
c_byte | signed char | -128 to 127 |
c_ubyte | unsigned char | 0 to 255 |
c_char | char | One byte character, bytes of length 1 |
c_char holds a character as a one-byte bytes object, while c_byte holds a number. Pick the one that matches the meaning of the C declaration.
ctypes does not check ranges for integer types. A value that does not fit is truncated to the low byte and reinterpreted as signed:
This matches C behaviour for conversions and is a frequent source of bugs. If a value could exceed the range, validate it in Python before storing it, or use c_ubyte when the data is really unsigned.
Declare c_byte in argtypes and restype when a C function uses signed char (also written int8_t in stdint.h):
The library name is an example, so compile your own C source into a shared library to try it. Python also provides c_int8 as an alias for the signed one-byte integer type on platforms where it exists. Prefer c_int8 when the C code uses int8_t, since the name states the intent and width.
Multiply the type to get a fixed-size array. This is how you model a small binary buffer or a field in a packed record:
To copy the raw bytes of an array into a Python bytes object, use ctypes.string_at(ctypes.addressof(samples), ctypes.sizeof(samples)). For unsigned data, bytes(array_of_c_ubyte) is simplest.
Sometimes data arrives as signed bytes but you need the unsigned value. Mask with 0xFF in Python, or reinterpret through a c_ubyte:
Both give the same result because the stored bit pattern is identical. Only the interpretation differs.
c_byte is meant for interoperating with C memory. For parsing or building binary data entirely in Python, bytes, bytearray, memoryview and the struct module are usually clearer:
Reach for c_byte when the memory is shared with C, as with a structure passed by byref() or a field you fill from a library.
Pick the ctypes type by the C declaration, not by what looks convenient. A char that holds text maps to c_char, a signed char or int8_t maps to c_byte or c_int8, and an unsigned char or uint8_t maps to c_ubyte or c_uint8. Plain C char has implementation-defined signedness, so check the target platform before treating it as a number.
-128 to 127, because it is a signed single byte.
ctypes does not raise an error. It wraps the value, and c_byte(200).value is -56.
c_byte is signed, with a range of -128 to 127. c_ubyte is unsigned, with a range of 0 to 255.
No. c_byte holds a small integer, while c_char holds a single character as a one-byte bytes value.
ctypes.c_byte maps the C signed char to Python as a one byte integer from -128 to 127. Values that do not fit wrap around silently, so use c_ubyte for unsigned data, c_char for characters and c_int8 when the C code uses int8_t. Declare it in argtypes, restype and structures to match the C signature.