Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert Protobuf ByteString to String in java

Could anyone please let me know how to convert protobuf's ByteString to an octal escape sequence String in java?

In my case, I am getting the ByteString value as \376\024\367 so, when I print the string value in console using System.out.println(), I should get "\376\024\367".

Many thanks.

like image 871
Dinesh Kumar J Avatar asked Jun 02 '17 09:06

Dinesh Kumar J


People also ask

What is ByteString java?

Encodes text into a sequence of bytes using the named charset and returns the result as a ByteString . static ByteString. copyFrom(java.lang.String text, java.lang.String charsetName) Encodes text into a sequence of bytes using the named charset and returns the result as a ByteString .


1 Answers

Normally, you'd convert a ByteString to a String using ByteString#toString(Charset). This method lets you specify what charset the text is encoded in. If it's UTF-8, you can also use the method toStringUtf8() as a shortcut.

From your question, though, it sounds like you actually want to produce the escaped format using C-style three-digit octal escapes. AFAIK there's no public function to do this, but you can see the code here. You could copy that code into your own project and use it.

like image 190
Kenton Varda Avatar answered Oct 27 '22 08:10

Kenton Varda