3

I am trying to define an eight byte MAC address data element using protocol buffers in C. I have tried:

message mac {
    required bytes address = 1 [(nanopb).max_size = 8];
}

but this creates a structure with a size field and an eight byte address field. I would like just and eight byte address field.

I rejected using a fixed64 element as a MAC address is a sequence of bytes. Sorry if this is a simple question as I am fairly new to protocol buffers.

jpa
  • 10,351
  • 1
  • 28
  • 45
Bryan
  • 31
  • 2

1 Answers1

2

The size field is generated because there is no way to force a minimum length on a 'bytes' field. So if whoever created the message didn't give 8 bytes in the field, the rest would be garbage data.

Does the size field cause some trouble? It does certainly consume 2-4 bytes of RAM, but that's all.

(I'm the author of nanopb; though probably this is the same in all the protobuf implementations.)

Update: Since nanopb 0.3.8 (released in 2017) there is now support for fixed length bytes fields using the option (nanopb).fixed_length = true.

jpa
  • 10,351
  • 1
  • 28
  • 45
  • I am attempting to make this equivalent with already existing code and was wondering if there was a way to make the field a fixed size. Thanks for the reply. – Bryan Jul 17 '12 at 17:22