假设proto中定义了一个enum枚举:
enum UserStatus {
AWAY = 0;
ONLINE = 1;
OFFLINE = 2;
}
方法一:
std::string name = UserStatus_Name(UserStatus::AWAY);
(如果UserStatus嵌套在message类型MyMessage内,则使用MyMessage_UserStatus_Name(...))
name的值就是"AWAY"
方法二:
可以通过以下方法转换name和value:
const google::protobuf::EnumDescriptor *descriptor = UserStatus_descriptor();
std::string name = descriptor->FindValueByNumber(UserStatus::ONLINE)->name();
int number = descriptor->FindValueByName("ONLINE")->number();
reference:
https://stackoverflow.com/questions/32161409/how-to-get-protobuf-enum-as-string/46470954#46470954