lacas
lacas

Reputation: 14066

Check whether bytes in a byte array are Unicode

How can I check whether bytes in a byte array are Unicode characters?

main probleme:

android 4.2 new String(bytes) remove unicode chars from my String: [\uFFFD]

I need a workaround for this.

Upvotes: 0

Views: 1987

Answers (2)

Louis Wasserman
Louis Wasserman

Reputation: 198103

return Charset.forName("UTF-8").newEncoder().canEncode(string);

UPDATE: You didn't pass a Charset to new String(bytes), so it just assumes you're using the default charset. Instead, do e.g. new String(bytes, "UTF-8").

Upvotes: 2

prprcupofcoffee
prprcupofcoffee

Reputation: 2970

No, not really. You can guess, by observing that the characters in the byte array don't violate UTF-8 rules, for example. See http://blogs.msdn.com/b/oldnewthing/archive/2007/04/17/2158334.aspx for some more information.

Upvotes: 1

Related Questions