Guillaume Brunerie
Guillaume Brunerie

Reputation: 5371

Convert a stream of bytes to a String (UTF-8 encoded)

I have a stream of bytes (given by a BufferedInputStream) which represents a sequence of UTF-8 strings. The protocol is a little crappy becauses it uses 0xFF between commands, which implies that the whole stream is not a valid UTF-8 string, only the parts between the 0xFF are.

How can I extract those UTF-8 strings.

I don’t think BufferedReader would work, because the 0xFF will mess up everything and I have found no byte array to UTF-8 string conversion function.

Upvotes: 2

Views: 11353

Answers (1)

Leonard Brünings
Leonard Brünings

Reputation: 13222

byte[] to String

So would be new String(bytes, "UTF-8");

You would need to filter the Stream beforehand though.

Upvotes: 2

Related Questions