Example of decoding a string

42 views
Skip to first unread message

Jen

unread,
Dec 18, 2024, 1:21:18 AM12/18/24
to nanopb
Hello,

Does anyone know where I can find an example of decoding a string? My .proto file has two int_32 and a string like so:

syntax = "proto3";

message DigitiserConfig {
  int32 digitiser_block_size = 1;
  int32 max_us_timing_error = 2;
  string file_name_base = 3;
}

I am able to very simply decode the integers like so:

DigitiserConfig digitiserConfig = DigitiserConfig_init_zero;

pb_istream_t stream = pb_istream_from_buffer((uint8_t *)buffer, actual_size);


if (!pb_decode(&stream, DigitiserConfig_fields, &digitiserConfig)) {

printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));

return FX_FILE_CORRUPT; // Return error if decoding fails

}


and then access them from the digitiserConfig struct.

But the file_name_base is a pb_callback_t and I just can't figure out what I'm meant to do with that.

In the API reference it says:
" For decoding strings and bytes fields, the length has already been decoded and the callback function is given a length-limited substream. You can therefore check the total length in stream->bytes_left and read the data using pb_read."

I'm sure it's really basic but I just can't figure it out or find anything on this. I hope someone can point me in the right direction.

Thanks,
Jen

Petteri Aimonen

unread,
Dec 18, 2024, 1:24:07 AM12/18/24
to nanopb
Hi,

The easiest way is to specify the maximum size for the string.
Then nanopb will make a char[] array to store it.

See for example
and the documentation

--
Petteri

Jen Scrivener

unread,
Dec 20, 2024, 1:20:11 AM12/20/24
to nan...@googlegroups.com

Brilliant, that's just what I was after. Thank you!


--
You received this message because you are subscribed to the Google Groups "nanopb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nanopb+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nanopb/27f5c1e1-b9bb-4b41-8457-c0fbd10c2a6fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages