Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode byte[] in proto buffs

Is there an equivalent to int8 or byte type in proto buffs? I would like to send a byte array object.

like image 767
Usman Ismail Avatar asked Dec 07 '11 19:12

Usman Ismail


People also ask

What encoding does protobuf use?

Protobuf strings are always valid UTF-8 strings. See the Language Guide: A string must always contain UTF-8 encoded or 7-bit ASCII text.

Is protobuf binary format?

Protobuf is a binary message format crafted by Google and is efficient compared to other message formats like JSON & XML.

Can you compress protobuf?

Protobuf achieves good compression ratios through varint encoding. It might not be necessary to add an extra layer of compression in most cases. However, to arrive at a payload size equivalent to the compressed JSON payload size, we applied ZSTD compression to the Protobuf object.


2 Answers

http://code.google.com/apis/protocolbuffers/docs/proto.html#scalar

bytes 
like image 198
Luka Rahne Avatar answered Oct 15 '22 18:10

Luka Rahne


If you are looking to store a single byte, however, I would suggest using the Int32. It is a 'variant' type variable that will change size depending on the data that is stored in it. So if you are storing a single byte, it will be the smallest structure. The byteS data type is actually quite large in comparison. This is because the bytes structure holds data such as index length and other properties.

On serialization, I noticed a size difference of about half when switching from a single byte stored in a byteS to an int32.

Note, however, that this does not apply to multiple bytes being stored in the byteS, which I suspect will be much smaller than storing individual Int32.

like image 36
Misha Mengisen Avatar answered Oct 15 '22 18:10

Misha Mengisen