byte[]转string

java的byte[]与String相互转换

心已入冬 提交于 2019-11-28 07:12:39
String转byte[] byte[] sInput = new byte[0]; try { // 可以指定编码,默认也只UTF-8 sInput = "这是内容".getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } byte[]转String // 这里sInput是上面byte[],也是可以指定编码,默认也是UTF-8 String str = new String(sInput, "UTF-8") byte[]打印到控制台看 org.bouncycastle.pqc.math.linearalgebra.ByteUtils.toBinaryString(sInput) // 这个方法的源码 public static String toBinaryString(byte[] var0) { String var1 = ""; for(int var2 = 0; var2 < var0.length; ++var2) { byte var3 = var0[var2]; for(int var4 = 0; var4 < 8; ++var4) { int var5 = var3 >>> var4 & 1; var1 = var1 + var5; } if

[转] 有点难度的java笔试题

可紊 提交于 2019-11-27 07:31:52
---Question--- 1.What is the output of the following program? public class Foo { public static void main(String[] args){ Map<byte[], String> m = new HashMap<byte[], String>(); byte[] key = "abcd".getBytes(); m.put(key, "abcd"); System.out.println(m.containsKey(key)); System.out.println(m.containsKey("abcd")); System.out.println(m.containsKey("abcd".getBytes())); } } a) true,true,false b)true,false,false c)true,true,true d) false,false,false e)Program throws an exception 2. What is the proper string filled in the following program? Public class Foo { public static void main(String[] args) {