NanoPB: How to handle string types in C++
See also: C version: How to handle string types in C
NanoPB is a code-size optimized Protocol Buffers implementation for embedded systems. This post shows how to handle string types in C++ with NanoPB.
Proto definition
First, create a .proto file with string fields:
syntax = "proto3";
package example;
message StringMessage {
string name = 1;
string description = 2;
}Generate NanoPB code
Generate the NanoPB code with a .options file to specify string buffer sizes:
Create strings.options:
example.StringMessage.name max_size:64
example.StringMessage.description max_size:256Then generate:
protoc --nanopb_out=. strings.protoThis will generate strings.pb.h and strings.pb.c.
C++ example with fixed-size buffers
Here’s a complete C++ example using fixed-size buffers:
#include <stdio.h>
#include <string.h>
#include "strings.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_StringMessage message = example_StringMessage_init_zero;
// Set string values (must fit in buffer size)
const char* name = "NanoPB";
const char* description = "Protocol Buffers for embedded systems";
strncpy(message.name, name, sizeof(message.name) - 1);
strncpy(message.description, description, sizeof(message.description) - 1);
// Create stream for encoding
pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
// Encode the message
if (!pb_encode(&ostream, example_StringMessage_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_StringMessage decoded = example_StringMessage_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_StringMessage_fields, &decoded)) {
printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
return 1;
}
// Print decoded values
printf("Decoded values:\n");
printf(" name: %s\n", decoded.name);
printf(" description: %s\n", decoded.description);
return 0;
}Compile command
Compile the example with nanopb. NanoPB is typically used by including the source files directly in your project:
g++ -o strings_example strings_example.cpp strings.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 strings_pb2
# Read the binary data
with open('encoded.bin', 'rb') as f:
data = f.read()
# Decode
msg = strings_pb2.StringMessage()
msg.ParseFromString(data)
print("Python decoded values:")
print(f" name: {msg.name}")
print(f" description: {msg.description}")First, compile the Python protobuf definitions:
protoc --python_out=. strings.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 strings
For dynamic string handling, you can use callbacks. Create strings_callback.options:
# Use callback for dynamic strings
msg.StringMessage.name callback
msg.StringMessage.description callbackThen regenerate and use this approach:
#include <stdio.h>
#include <string>
#include "strings.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
// Encoder callback for strings
bool string_encode_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
const std::string* str = (const std::string*)*arg;
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, (const pb_byte_t*)str->c_str(), str->size());
}
// Decoder callback for strings
bool string_decode_callback(pb_istream_t *stream, const pb_field_t *field, void **arg) {
std::string* str = (std::string*)*arg;
// Allocate buffer
size_t size = stream->bytes_left;
char* buf = new char[size];
if (!pb_read(stream, (pb_byte_t*)buf, size)) {
delete[] buf;
return false;
}
str->assign(buf, size);
delete[] buf;
return true;
}
int main() {
uint8_t buffer[256];
size_t message_length;
std::string name = "NanoPB";
std::string description = "Protocol Buffers for embedded systems";
// --- ENCODING ---
example_StringMessage message = example_StringMessage_init_zero;
message.name.funcs.encode = string_encode_callback;
message.name.arg = &name;
message.description.funcs.encode = string_encode_callback;
message.description.arg = &description;
pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
if (!pb_encode(&ostream, example_StringMessage_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_StringMessage decoded = example_StringMessage_init_zero;
std::string decoded_name, decoded_description;
decoded.name.funcs.decode = string_decode_callback;
decoded.name.arg = &decoded_name;
decoded.description.funcs.decode = string_decode_callback;
decoded.description.arg = &decoded_description;
pb_istream_t istream = pb_istream_from_buffer(buffer, message_length);
if (!pb_decode(&istream, example_StringMessage_fields, &decoded)) {
printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
return 1;
}
printf("Decoded values:\n");
printf(" name: %s\n", decoded_name.c_str());
printf(" description: %s\n", decoded_description.c_str());
return 0;
}Key points
- Fixed-size buffers: Use
max_sizein .options file for simple, static allocation - Callback-based: Use
callbackin .options for dynamic string handling - Fixed-size: Use
strncpyto copy strings, ensure null-termination - Callback-based: Implement encode/decode callbacks for dynamic allocation
- Always check buffer sizes to prevent overflow
- Callback approach allows strings of any size (within memory limits)
When to use which approach
- Fixed-size buffers: When you know maximum string sizes and want simple code
- Callback-based: When string sizes are variable or you need dynamic memory allocation
Expected output
Encoded 38 bytes
Encoded data: 0a 06 4e 61 6e 6f 50 42 12 1e 50 72 6f 74 6f 63 6f 6c 20 42 75 66 66 65 72 73 20 66 6f 72 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73
Decoded values:
name: NanoPB
description: Protocol Buffers for embedded systemsMore NanoPB posts
- Basic scalar types in C++
- Basic scalar types in C
- String types in C
- Bytes types in C++
- Bytes types in C
- Optional fields in C++
- Optional fields in C
- Repeated fields/arrays in C++
- Repeated fields/arrays in C
- Enums in C++
- Enums in C
- Nested messages in C++
- Nested messages in C
- Oneof/union types in C++
- Oneof/union types in C
- Custom array converters in C++
- Custom array converters in C