字节缓冲区输出 Android Java

Bytebuffer output Android Java

我想显示我的虚拟数据的前 4 个字符。 我怎么做? 我想要作为输出:

outputvalue = $*
outputvalue = 22



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    byte[] outData = ("$*220000000000101SDFRGGHYB0").getBytes();
    ByteBuffer b = ByteBuffer.wrap(outData);
    System.out.println("The structure"+ b);
    outputvalue1 = $*
    outputvalue2 = 22



}
byte[] outData = "$*220000000000101SDFRGGHYB0".getBytes("UTF-8");
ByteBuffer b = ByteBuffer.wrap(outData);

byte[] outputValue1 = new byte[2];
b.get(outputValue1);

byte[] outputValue2 = new byte[2];
b.get(outputValue2);

String outputString1 = new String(outputValue1, "UTF-8");
String outputString2 = new String(outputValue2, "UTF-8");
int outputNum2 = Integer.parseInt(outputString2);

字符串始终采用 Unicode,而作为文本的二进制数据 (byte[]) 具有一些编码、字符集。在 String/Reader/Writerbyte[]/InputStream/OutputStream

之间转换时必须提到这一点

ByteBuffer 允许定位、随机访问读写。以上使用线性读数。