.proto Type
Notes
C++ Type
Java Type
PythonType[2]
double
double
double
float
float
float
float
float
int32
Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead.
int32
int
int
int64
Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead.
int64
long
int/long[3]
uint32
Uses variable-length encoding.
uint32
int[1]
int/long[3]
uint64
Uses variable-length encoding.
uint64
long[1]
int/long[3]
sint32
Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.
int32
int
int
sint64
Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.
int64
long
int/long[3]
fixed32
Always four bytes. More efficient than uint32 if values are often greater than 228.
uint32
int[1]
int
fixed64
Always eight bytes. More efficient than uint64 if values are often greater than 256.
uint64
long[1]
int/long[3]
sfixed32
Always four bytes.
int32
int
int
sfixed64
Always eight bytes.
int64
long
int/long[3]
bool
bool
boolean
boolean
string
A string must always contain UTF-8 encoded or 7-bit ASCII text.
string
String
str/unicode[4]
bytes
May contain any arbitrary sequence of bytes.
string
ByteString
str
如何编译proto文件 在windows如何下载并编译protobuff,这部分参考博客:http://kuaile.in/archives/1214 github仓库地址:https://github.com/google/protobuf Google下载protobuff下载地址:https://developers.google.com/protocol-buffers/docs/downloads 。 在解压后的文件夹中,打开vsprojects目录,目录中的文件如图所示: 分别依次右键编译生成libprotobuf.lib,libprotobuf-lite.lib,libprotoc.lib,protoc.exe 到这里就完成了protobuff的编译。 打开cmd,cd到protoc.exe所在的文件夹,即../vsproject/。假设E:\proto\存在myproto.proto文件。输入 [html] view plain copy protoc -I=e:\proto --cpp_out=e:\proto e:\proto\myproto.proto 就会在e:\proto 下生成myproto.h 和myproto.cpp文件,测试成功就说明protobuff已经安装成功。 如何将proto文件编译成.cs文件 接下来要将proto文件编译成.cs文件。.net版的protobuf来源于proto社区,有两个版本: 一个版本叫protobuf-net,下载地址为:http://code.google.com/p/protobuf-net/ 写法上比较符合c#一贯的写法,而且效率更高。 另一个为protobuf-csharp-sport , 官方站点:http://code.google.com/p/protobuf-csharp-port/ 写法上跟java上的使用极其相似,比较遵循Google 的原生态写法,跨平台选择此版本比较好。 这里使用的是protobuf-net,下载解压后,将 Precompile\precompile.exe 以及 ProtoGen\protogen.exe 两个文件加入到环境变量中,打开cmd, 使用方法如下: [html] view plain copy //使用方法为: [html] view plain copy protogen -i:input.proto -o:output.cs protogen -i:input.proto -o:output.xml -t:xml protogen -i:input.proto -o:output.cs -p:datacontract -q protogen -i:input.proto -o:output.cs -p:observable=true 以E:\proto\myproto.proto为例,cmd下输入 [html] view plain copy protogen -i:e:\proto\myproto.proto -o:myproto.cs 运行成功后就会在文件夹内生成myproto.cs文件 如何将myproto.cs加入你的项目中 有两种方法可以将.cs文件加入到你的项目中:第一种就是预编译成.dll文件,然后加入到你的项目中就可以了。第二种就是直接使用源码(这样的话编译速度会比较慢),将你的.cs文件直接加入到你的项目中,然后在项目中引用protobuf-net.dll的库,就可以使用了。 参考文章: 在ios android设备上使用 Protobuf (使用源码方式) http://game.ceeger.com/forum/read.php? tid=14359&fid=27&page=1 android与PC,C#与Java利用protobuf 进行无障碍通讯 http://www.cnblogs.com/TerryBlog/archive/2011/04/23/2025654.html windows下Google Protocol Buffer 编译安装教程 http://kuaile.in/archives/1214