I am new to protobuf and want to experiment with it, by passing some data over the network or in a file, such as
2-byte unsigned int: 15
2-byte unsigned int: 15 and -1
4-byte unsigned int: 256, and then a string "Peter"
4-byte unsigned int: 256, and then two strings "Peter", "Mary"
4-byte signed int: (3, 4) as a point
4-byte signed int: a point above twice, such as (3, 4) and (10, 11) as a line
4-byte signed int and a string: the line above, and a name for this line
Can the bytes be written by Python / Ruby in a file, and then read back by JavaScript? (or it can be all written in JavaScript).
I think being able to pass it on a local website is probably quite a bit more complicated? If so, writing it to a file and be able to read it back would be fine. How could it be done?
I found that it is a lot simpler to use protobuf.js, and we don't need to use
protoc
which is a compiler command line tool. I found it difficult to make the Python or the C++ version to work, as it involves Python2 vs Python3, andpip
and missing libraries.So simply follow the protobuf.js website, and I have included the basic minimum to get it to work.
We can read all about how to make a
.proto
file from Google's website.Steps:
Create an empty folder and install protobufjs. Google for
nvm
(optional) and Node.js and npm if you need to.Now, create these 3 files:
Now run the file
write.js
and it will create a data file:
awesome.dat
.On the Mac, you can hex dump it to look at it:
and now, to "get the data back", use
If I use Node.js v14, it didn't work for me on a MacBook Air M1 for some reason, but Node v15 and v16 worked.
Also, note that when we write the file and read the file in Node.js, we don't specify the encoding due to the following reasons: