I want to convert a string object to ByteString.I have tried to use ByteString.CopyFrom() function to convert,but the return value is always "{Google.ProtocolBuffers.ByteString}".Why? How can I do?
The function i use like this.
The result
The Protobuf serialization mechanism is given through the protoc application, this compiler will parse the . proto file and will generate as output, source files according to the configured language by its arguments, in this case, C++. You can also obtain more information about, reading the section compiler invocation.
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.
Protobuf categorizes normally used data types into wire types which provides information to find the length of the value. For wire type 2, length is also encoded along with value. Zero values are not transmitted. 0, False, “”, does not take any bytes.
According to the docs, Google.ProtocolBuffers.ByteStream
needs an encoding to know how to display its content. Use ByteStream.ToString(Encoding encoding)
or ByteStream.ToStringUtf8()
.
You can use one of methods from ByteString class to convert string to ByteArray
ByteString.copyFromUtf8(stringText)
.
Your string
has been successfully converted to ByteStream
. If you see {Google.ProtocolBuffers.ByteString} in the watch window, it simply means that the ByteStream
does not override the ToString
method. In short, Visual Studio doesn't know how to display a ByteStream
, and therefore just display the type name instead.
That said, there is an overload of the CopyFrom
method that allows you to directly use a string:
var APP_DEF_TEA_KEY = ByteString.CopyFrom("e#>&*m16", Encoding.Unicode);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With