Boreas
Boreas

Reputation: 163

How to convert string to ByteString when using protobuf?

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

Upvotes: 13

Views: 47123

Answers (3)

Maneesh K Bishnoi
Maneesh K Bishnoi

Reputation: 139

You can use one of methods from ByteString class to convert string to ByteArray ByteString.copyFromUtf8(stringText).

Upvotes: 3

quimnuss
quimnuss

Reputation: 1561

According to the docs, Google.ProtocolBuffers.ByteStreamneeds an encoding to know how to display its content. Use ByteStream.ToString(Encoding encoding) or ByteStream.ToStringUtf8().

Upvotes: 3

Kevin Gosse
Kevin Gosse

Reputation: 39007

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);

Upvotes: 14

Related Questions