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:

bytes.proto
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.txt
example.BytesMessage.data max_size:32
example.BytesMessage.signature max_size:16

Then generate:

example.sh
protoc --nanopb_out=. bytes.proto

This 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:

bytes_example.cpp
#include <stdio.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:

example.sh
g++ -o bytes_example bytes_example.cpp 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:

test_bytes.py
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:

example.sh
protoc --python_out=. bytes.proto

Then modify the C++ example to save the encoded data to a file:

example.cpp
// 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:

example.txt
# Use callback for dynamic bytes
msg.BytesMessage.data callback
msg.BytesMessage.signature callback

Then regenerate and use this approach:

bytes_callback_example.cpp
#include <stdio.h>
#include <vector>
#include "bytes.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"

// Encoder callback for bytes
bool bytes_encode_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
    const std::vector<uint8_t>* data = (const std::vector<uint8_t>*)*arg;
    
    if (!pb_encode_tag_for_field(stream, field))
        return false;
    
    return pb_encode_string(stream, data->data(), data->size());
}

// Decoder callback for bytes
bool bytes_decode_callback(pb_istream_t *stream, const pb_field_t *field, void **arg) {
    std::vector<uint8_t>* data = (std::vector<uint8_t>*)*arg;
    
    // Resize vector to hold the data
    size_t size = stream->bytes_left;
    data->resize(size);
    
    // Read data into vector
    return pb_read(stream, data->data(), size);
}

int main() {
    uint8_t buffer[256];
    size_t message_length;
    
    std::vector<uint8_t> data = {0x01, 0x02, 0x03, 0x04, 0x05};
    std::vector<uint8_t> signature = {0xAA, 0xBB, 0xCC, 0xDD};
    
    // --- 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;
    std::vector<uint8_t> decoded_data, decoded_signature;
    
    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 (uint8_t byte : decoded_data) {
        printf("%02x ", byte);
    }
    printf("\n");
    printf("  signature: ");
    for (uint8_t byte : decoded_signature) {
        printf("%02x ", byte);
    }
    printf("\n");
    
    return 0;
}

Key points

When to use which approach

Expected output

example.txt
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 

Use cases for bytes

example.txt

## 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-c-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/)

Check out similar posts by category: Embedded, C/C++, Protocol Buffers