// 乱码字符串编码转换 String luanma = "小王"; //将utf8 转为 iso String ISO_s = new String(luanma.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1); // 此时ISO_s 会显示乱码 System.out.println(ISO_s); // 将iso转iso去接收才能显示正确 因为ISO_s本来就是iso String luanma2 = new String(ISO_s.getBytes(StandardCharsets.ISO_8859_1)); // 显示正常 System.out.println(luanma2); // 方法二 将iso 编码再转回utf8 String luanma3 = new String(ISO_s.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8); // 显示正常 System.out.println(luanma3);
// java 编码问题 String bianma = "小王"; // 将 utf-8 编码转 ISO_8859_1 String bianma2 = new String(bianma.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1); System.out.println(bianma2); byte[] bytes = bianma.getBytes(StandardCharsets.UTF_8); String bianma3 = Arrays.toString(bytes); System.out.println(bianma3); byte[] bytes2 = bianma.getBytes(StandardCharsets.ISO_8859_1); String ISO_ = Arrays.toString(bytes2); System.out.println(ISO_); // 乱码字符串编码转换 String luanma = "小王"; //将utf8 转为 iso String ISO_s = new String(luanma.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1); System.out.println(ISO_s); // 将iso转iso才能显示正确 因为ISO_s本来就是iso String luanma2 = new String(ISO_s.getBytes(StandardCharsets.ISO_8859_1)); System.out.println(luanma2); // 方法二 将iso 编码再转回utf8 String luanma3 = new String(ISO_s.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8); System.out.println(luanma3); // 十六进制编码 String HexEncodeStr = HexBin.encode(bytes); System.out.println(HexEncodeStr); byte[] HexdecodeStr = HexBin.decode(HexEncodeStr); System.out.println(Arrays.toString(HexdecodeStr)); String yz = new String(HexdecodeStr); System.out.println(yz);