这样把ByteBuffer转成String是不是正确的做法,
String k = "abcd";
ByteBuffer b = ByteBuffer.wrap(k.getBytes());
String v = new String(b.array());
if(k.equals(v))
System.out.println("it worked");
else
System.out.println("did not work");
我问的原因是这看起来太简单了,而其他方法如 Java: Converting String to and from ByteBuffer and associated problems看起来更复杂。
最佳答案
Andy Thomas 提到,有一种更简单的方法可以毫无问题地将 ByteBuffer
解码为 String
。
String s = StandardCharsets.UTF_8.decode(byteBuffer).toString();
关于Java ByteBuffer 到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17354891/