Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Last active November 1, 2016 06:19
Show Gist options
  • Save dmikurube/a3659e09ab9b2dd48571d3a7d2f53627 to your computer and use it in GitHub Desktop.
Save dmikurube/a3659e09ab9b2dd48571d3a7d2f53627 to your computer and use it in GitHub Desktop.
UTF-8 Conversion in JDK 1.7 and 1.8
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.HashMap;
import java.util.Map;
public class UTF8PerJavaVersion {
public static void main(String[] args) throws CharacterCodingException {
byte[] b_org = new byte[] { (byte) 0xfd, (byte) 0xfc };
for (byte b_each : b_org) {
System.out.printf(" %02X", b_each);
}
System.out.println("");
String s = convertByteArrayToString(b_org);
System.out.println(s);
for (char c : s.toCharArray()) {
System.out.printf(" %04X", (int)c);
}
System.out.println("");
byte[] b_new = s.getBytes(charset);
for (byte b_each : b_new) {
System.out.printf(" %02X", b_each);
}
System.out.println("");
}
private static String convertByteArrayToString(byte[] b) throws CharacterCodingException {
CharsetDecoder decoder = Charset.forName("UTF-8")
.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
return decoder.decode(ByteBuffer.wrap(b)).toString();
}
private static final Charset charset = Charset.forName("UTF-8");
}