将 String 转换为 ByteBuffer,然后使用 Java 从 ByteBuffer 转换回 String:
import java.nio.charset.Charset;
import java.nio.*;
String babel = "obufscate thdé alphebat and yolo!!";
System.out.println(babel);
//Convert string to ByteBuffer:
ByteBuffer babb = Charset.forName("UTF-8").encode(babel);
try{
//Convert ByteBuffer to String
System.out.println(new String(babb.array(), "UTF-8"));
}
catch(Exception e){
e.printStackTrace();
}
首先打印打印的裸字符串,然后将 ByteBuffer 转换为 array():
obufscate thdé alphebat and yolo!!
obufscate thdé alphebat and yolo!!
这对我也很有帮助,将字符串减少为原始字节可以帮助检查发生了什么:
String text = "こんにちは";
//convert utf8 text to a byte array
byte[] array = text.getBytes("UTF-8");
//convert the byte array back to a string as UTF-8
String s = new String(array, Charset.forName("UTF-8"));
System.out.println(s);
//forcing strings encoded as UTF-8 as an incorrect encoding like
//say ISO-8859-1 causes strange and undefined behavior
String sISO = new String(array, Charset.forName("ISO-8859-1"));
System.out.println(sISO);
打印解释为 UTF-8 的字符串,然后再打印为 ISO-8859-1:
こんにちは
ããã«ã¡ã¯