在运行时,我从设备获取字节缓冲区数据,我试图解码该数据以读取其中的内容。
当我使用字符串打印字节缓冲区时,它显示如下,
java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
我尝试使用以下所有已知格式进行解码,
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(paramByteBuffer);
String text = charBuffer.toString();
System.out.println("UTF-8"+text);
charBuffer = StandardCharsets.UTF_16.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("UTF_16"+text);
charBuffer = StandardCharsets.ISO_8859_1.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("ISO_8859_1"+text);
charBuffer = StandardCharsets.UTF_16BE.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("UTF_16BE"+text);
charBuffer = StandardCharsets.UTF_16LE.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("UTF_16LE"+text);
charBuffer = StandardCharsets.US_ASCII.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("US_ASCII"+text);
一切都返回空数据。
字节缓冲区数据的解码方式有哪些?
最佳答案
你可以这样做:
String val = new String(paramByteBuffer.array());
或
String val = new String(paramByteBuffer.array(),"UTF-8");
Here是支持的字符集列表
关于java - 将字节缓冲区转换为字符串在 Java 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30363843/