如何讀取ByteBuffer中存儲的數據?如何從ByteBuffer讀取數據
setValue()
- 得到值「12 10」,被轉換爲十六進制值,並存儲在數據String[]
。write()
- 將數據轉換爲字節並存儲在ByteBuffer
dest
中。readBuffer
- 如何從ByteBuffer
讀取數據?
static String[] data = {};
//value = "12 10";
String setValue(String value) {
String[] samples = value.split("[ ,\n]+");
data = new String[samples.length];
//Generates Hex values
for (int i = 0; i < samples.length; i++) {
samples[i] = "0x"+String.format("%02x", Byte.parseByte(samples[i]));
//data[i] will have values 0x0c, 0x0a
data[i] = samples[i];
}
System.out.println("data :: " +Arrays.toString(samples));
return value;
}
void write(int sequenceNumber, ByteBuffer dest) {
for (int i = 0; i < data.length; i++) {
System.out.println("data[i] in bytes :: "+data[i].getBytes());
dest.put(data[i].getBytes());
}
}
void readBuffer(ByteBuffer destValue)
{
//How to read the data stored in ByteBuffer?
}
您是否閱讀過文檔? – RealSkeptic
這是輸入:String dataValue =「12 10」;它被轉換爲十六進制,如0x0c,0x0a,並將其添加到數組String []中,然後像這樣將dest.put(data [i] .getBytes())添加到ByteBuffer中。現在我需要編寫一個讀取ByteBuffer「dest」並返回輸出0x0c,0x0a的函數。任何人都可以請建議如何做到這一點 –