String转byte[]

  1. byte[] sInput = new byte[0];
  2. try {
  3. // 可以指定编码,默认也只UTF-8
  4. sInput = "这是内容".getBytes("UTF-8");
  5. } catch (UnsupportedEncodingException e) {
  6. e.printStackTrace();
  7. }

byte[]转String

  1. // 这里sInput是上面byte[],也是可以指定编码,默认也是UTF-8
  2. String str = new String(sInput, "UTF-8")

byte[]打印到控制台看

  1. org.bouncycastle.pqc.math.linearalgebra.ByteUtils.toBinaryString(sInput)
  2. // 这个方法的源码
  3. public static String toBinaryString(byte[] var0) {
  4. String var1 = "";
  5. for(int var2 = 0; var2 < var0.length; ++var2) {
  6. byte var3 = var0[var2];
  7. for(int var4 = 0; var4 < 8; ++var4) {
  8. int var5 = var3 >>> var4 & 1;
  9. var1 = var1 + var5;
  10. }
  11. if (var2 != var0.length - 1) {
  12. var1 = var1 + " ";
  13. }
  14. }
  15. return var1;
  16. }