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 <stdint.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:
gcc -o strings_example strings_example.c 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 <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "strings.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
typedef struct {
char* data;
size_t size;
} dynamic_string_t;
// Encoder callback for strings
bool string_encode_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
const dynamic_string_t* str = (const dynamic_string_t*)*arg;
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, (const pb_byte_t*)str->data, str->size);
}
// Decoder callback for strings
bool string_decode_callback(pb_istream_t *stream, const pb_field_t *field, void **arg) {
dynamic_string_t* str = (dynamic_string_t*)*arg;
// Allocate buffer
size_t size = stream->bytes_left;
str->data = (char*)malloc(size + 1);
if (!pb_read(stream, (pb_byte_t*)str->data, size)) {
free(str->data);
str->data = NULL;
return false;
}
str->data[size] = '\0'; // Null-terminate
str->size = size;
return true;
}
int main() {
uint8_t buffer[256];
size_t message_length;
dynamic_string_t name = {"NanoPB", 6};
dynamic_string_t description = {"Protocol Buffers for embedded systems", 38};
// --- 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;
dynamic_string_t decoded_name = {NULL, 0};
dynamic_string_t decoded_description = {NULL, 0};
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.data);
printf(" description: %s\n", decoded_description.data);
// Free allocated memory
free(decoded_name.data);
free(decoded_description.data);
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 with manual memory management
- Always check buffer sizes to prevent overflow
- Remember to free allocated memory in callback-based approach
- C requires manual memory management (malloc/free) for dynamic strings
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 systemsDifferences from C++
The C version is nearly identical to the C++ version, with key differences:
- Use
malloc/freeinstead ofnew/deletefor dynamic memory - Manual memory management required (no destructors)
- Use struct for dynamic string wrapper instead of std::string
- Remember to null-terminate strings manually when needed
## 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/)
- [Bytes types in C++](/2026/05/09/nanopb-cpp-bytes-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/)