NanoPB: How to handle bytes types in C
See also: C++ version: How to handle bytes types in C++
NanoPB is a code-size optimized Protocol Buffers implementation for embedded systems. This post shows how to handle bytes types in C with NanoPB.
Proto definition
First, create a .proto file with bytes fields:
syntax = "proto3";
package example;
message BytesMessage {
bytes data = 1;
bytes signature = 2;
}Generate NanoPB code
Generate the NanoPB code with a .options file to specify bytes buffer sizes:
Create bytes.options:
example.BytesMessage.data max_size:32
example.BytesMessage.signature max_size:16Then generate:
protoc --nanopb_out=. bytes.protoThis will generate bytes.pb.h and bytes.pb.c.
C example with fixed-size buffers
Here’s a complete C example using fixed-size buffers:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "bytes.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
int main() {
// Buffer for encoded message
uint8_t buffer[256];
size_t message_length;
// --- ENCODING ---
example_BytesMessage message = example_BytesMessage_init_zero;
// Set bytes data
const uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
const uint8_t signature[] = {0xAA, 0xBB, 0xCC, 0xDD};
message.data.size = sizeof(data);
memcpy(message.data.bytes, data, sizeof(data));
message.signature.size = sizeof(signature);
memcpy(message.signature.bytes, signature, sizeof(signature));
// Create stream for encoding
pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
// Encode the message
if (!pb_encode(&ostream, example_BytesMessage_fields, &message)) {
printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
return 1;
}
message_length = ostream.bytes_written;
printf("Encoded %zu bytes\n", message_length);
// Print hex dump of encoded data
printf("Encoded data: ");
for (size_t i = 0; i < message_length; i++) {
printf("%02x ", buffer[i]);
}
printf("\n");
// --- DECODING ---
example_BytesMessage decoded = example_BytesMessage_init_zero;
// Create stream for decoding
pb_istream_t istream = pb_istream_from_buffer(buffer, message_length);
// Decode the message
if (!pb_decode(&istream, example_BytesMessage_fields, &decoded)) {
printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
return 1;
}
// Print decoded values
printf("Decoded values:\n");
printf(" data: ");
for (size_t i = 0; i < decoded.data.size; i++) {
printf("%02x ", decoded.data.bytes[i]);
}
printf("\n");
printf(" signature: ");
for (size_t i = 0; i < decoded.signature.size; i++) {
printf("%02x ", decoded.signature.bytes[i]);
}
printf("\n");
return 0;
}Compile command
Compile the example with nanopb. NanoPB is typically used by including the source files directly in your project:
gcc -o bytes_example bytes_example.c bytes.pb.c pb_common.c pb_encode.c pb_decode.c -I.Note: NanoPB source files (pb_common.c, pb_encode.c, pb_decode.c) need to be compiled directly with your project. You can obtain these from the NanoPB GitHub repository.
Python test script
To verify the encoding, you can use Python’s protobuf library:
import bytes_pb2
# Read the binary data
with open('encoded.bin', 'rb') as f:
data = f.read()
# Decode
msg = bytes_pb2.BytesMessage()
msg.ParseFromString(data)
print("Python decoded values:")
print(f" data: {msg.data.hex()}")
print(f" signature: {msg.signature.hex()}")First, compile the Python protobuf definitions:
protoc --python_out=. bytes.protoThen modify the C example to save the encoded data to a file:
// After encoding, add this:
FILE *f = fopen("encoded.bin", "wb");
fwrite(buffer, 1, message_length, f);
fclose(f);Alternative: Callback-based bytes
For dynamic bytes handling, you can use callbacks. Create bytes_callback.options:
# Use callback for dynamic bytes
msg.BytesMessage.data callback
msg.BytesMessage.signature callbackThen regenerate and use this approach:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "bytes.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
typedef struct {
uint8_t* data;
size_t size;
} dynamic_bytes_t;
// Encoder callback for bytes
bool bytes_encode_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
const dynamic_bytes_t* bytes = (const dynamic_bytes_t*)*arg;
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, bytes->data, bytes->size);
}
// Decoder callback for bytes
bool bytes_decode_callback(pb_istream_t *stream, const pb_field_t *field, void **arg) {
dynamic_bytes_t* bytes = (dynamic_bytes_t*)*arg;
// Allocate buffer
size_t size = stream->bytes_left;
bytes->data = (uint8_t*)malloc(size);
if (!bytes->data) {
return false;
}
bytes->size = size;
// Read data into buffer
if (!pb_read(stream, bytes->data, size)) {
free(bytes->data);
bytes->data = NULL;
bytes->size = 0;
return false;
}
return true;
}
int main() {
uint8_t buffer[256];
size_t message_length;
uint8_t data_array[] = {0x01, 0x02, 0x03, 0x04, 0x05};
uint8_t signature_array[] = {0xAA, 0xBB, 0xCC, 0xDD};
dynamic_bytes_t data = {data_array, sizeof(data_array)};
dynamic_bytes_t signature = {signature_array, sizeof(signature_array)};
// --- ENCODING ---
example_BytesMessage message = example_BytesMessage_init_zero;
message.data.funcs.encode = bytes_encode_callback;
message.data.arg = &data;
message.signature.funcs.encode = bytes_encode_callback;
message.signature.arg = &signature;
pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
if (!pb_encode(&ostream, example_BytesMessage_fields, &message)) {
printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
return 1;
}
message_length = ostream.bytes_written;
printf("Encoded %zu bytes\n", message_length);
// --- DECODING ---
example_BytesMessage decoded = example_BytesMessage_init_zero;
dynamic_bytes_t decoded_data = {NULL, 0};
dynamic_bytes_t decoded_signature = {NULL, 0};
decoded.data.funcs.decode = bytes_decode_callback;
decoded.data.arg = &decoded_data;
decoded.signature.funcs.decode = bytes_decode_callback;
decoded.signature.arg = &decoded_signature;
pb_istream_t istream = pb_istream_from_buffer(buffer, message_length);
if (!pb_decode(&istream, example_BytesMessage_fields, &decoded)) {
printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
return 1;
}
printf("Decoded values:\n");
printf(" data: ");
for (size_t i = 0; i < decoded_data.size; i++) {
printf("%02x ", decoded_data.data[i]);
}
printf("\n");
printf(" signature: ");
for (size_t i = 0; i < decoded_signature.size; i++) {
printf("%02x ", decoded_signature.data[i]);
}
printf("\n");
// Free allocated memory
free(decoded_data.data);
free(decoded_signature.data);
return 0;
}Key points
- Fixed-size buffers: Use
max_sizein .options file for simple, static allocation - Callback-based: Use
callbackin .options for dynamic bytes handling - Fixed-size: Set
*_sizefield and usememcpyto copy bytes - Callback-based: Implement encode/decode callbacks with manual memory management
- Bytes are similar to strings but contain raw binary data (no null-termination)
- Use
malloc/freein C for dynamic bytes handling - Always check buffer sizes to prevent overflow
- Remember to free allocated memory in callback-based approach
When to use which approach
- Fixed-size buffers: When you know maximum bytes size and want simple code
- Callback-based: When bytes size is variable or you need dynamic memory allocation
Expected output
Encoded 14 bytes
Encoded data: 0a 05 01 02 03 04 05 12 04 aa bb cc dd
Decoded values:
data: 01 02 03 04 05
signature: aa bb cc dd Differences from C++
The C version is nearly identical to the C++ version, with key differences:
- Use
malloc/freeinstead ofstd::vectorfor dynamic bytes - Manual memory management required (no destructors)
- Use struct for dynamic bytes wrapper instead of std::vector
- Remember to free allocated memory after use
- No automatic memory management in C
Use cases for bytes
- Binary data (images, audio, etc.)
- Cryptographic signatures
- Raw sensor data
- Custom binary protocols
- Any data that shouldn’t be interpreted as text
## More NanoPB posts
- [Basic scalar types in C++](/2026/05/09/nanopb-cpp-basic-scalar-types/)
- [Basic scalar types in C](/2026/05/09/nanopb-c-basic-scalar-types/)
- [String types in C++](/2026/05/09/nanopb-cpp-string-types/)
- [String types in C](/2026/05/09/nanopb-c-string-types/)
- [Bytes types in C++](/2026/05/09/nanopb-cpp-bytes-types/)
- [Optional fields in C++](/2026/05/09/nanopb-cpp-optional-fields/)
- [Optional fields in C](/2026/05/09/nanopb-c-optional-fields/)
- [Repeated fields/arrays in C++](/2026/05/09/nanopb-cpp-repeated-fields/)
- [Repeated fields/arrays in C](/2026/05/09/nanopb-c-repeated-fields/)
- [Enums in C++](/2026/05/09/nanopb-cpp-enums/)
- [Enums in C](/2026/05/09/nanopb-c-enums/)
- [Nested messages in C++](/2026/05/09/nanopb-cpp-nested-messages/)
- [Nested messages in C](/2026/05/09/nanopb-c-nested-messages/)
- [Oneof/union types in C++](/2026/05/09/nanopb-cpp-oneof-types/)
- [Oneof/union types in C](/2026/05/09/nanopb-c-oneof-types/)
- [Custom array converters in C++](/2026/05/09/nanopb-cpp-custom-array-converters/)
- [Custom array converters in C](/2026/05/09/nanopb-c-custom-array-converters/)