Transform UTF8 string to UCS-2 with replace invalid characters in java
问题 I have a sting in UTF8: "Red🌹🌹Röses" I need that to be converted to valid UCS-2(or fixed size UTF-16BE without BOM, they are the same things) encoding, so the output will be: "Red Röses" as the "🌹" out of range of UCS-2. What I have tried: @Test public void testEncodeProblem() throws CharacterCodingException { String in = "Red\uD83C\uDF39\uD83C\uDF39Röses"; ByteBuffer input = ByteBuffer.wrap(in.getBytes()); CharsetDecoder utf8Decoder = StandardCharsets.UTF_16BE.newDecoder(); utf8Decoder