What is the simplest way to use default values with bytes in nanopb?

248 Views Asked by At

I need to declare a message with one byte specifying a default value.

I tried that in .proto file:

message Message {
    required bytes test = 1 [default = 0];
}

but I get this error Expected String. because it expects a string as default value. Is there a way to consider bytes type as one byte in C (char, uint8_t, ...)?

I also tried to specify a string as default value ([default = '0']) but what I get is the corresponding ASCII conversion of the value specified, instead I need the real value preferably as hex value.

EDIT:

I made it work this way:

message Message {
    required bytes test = 1 [(nanopb).max_size = 1, (nanopb).fixed_length = true, default = "\xFF"];
}

I don't know if it is the correct way or just a workaround but it works.

1

There are 1 best solutions below

0
jpa On

default = "\xFF"

Yes, using hex escape codes is the correct way for binary data.

It's not very well documented in Google's protobuf docs, but AFAIK this is the way all protobuf implementations interpret the default value. The allowed escape types are in the specification.