Strings contain text, not arbitrary bytes. Encoded protocol buffers are arbitrary bytes, not text. So, they aren't compatible. You would need to do something like base-64 encode the data in order to put it in a String.
On Tue, Nov 24, 2009 at 3:16 PM, Will Morton <will.mor...@gmail.com> wrote: > Hello all; > > I need to serialize a protobuf message to a string so that it can be > passed outside my program. The below fails, I'm guessing due to UTF8 > encoding issues: > > byte[] arr = msg.toByteArray(); > String str = new String(arr); > // ... pass str around ... > MsgType msg2 = MsgType.parseFrom(str.getBytes()); // <-- throws > InvalidProtocolBufferException > > So, reading the API, I thought I should use ByteStrings, with their > handy UTF8 encoding methods, but this doesn't work either: > > ByteString bs = msg.toByteString(); > String str = bs.toStringUtf8(); > // ... pass str around ... > ByteString bs2 = ByteString.copyFromUtf8(str); > MsgType msg2 = MsgType.parseFrom(bs2); // <-- Still throws exception > > What am I doing wrong? What's the best way to do java string > serialization of protobuf messages? > > Thanks in advance, > > Will > > -- > > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To post to this group, send email to proto...@googlegroups.com. > To unsubscribe from this group, send email to > protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/protobuf?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.