在 Java 或 Kotlin 中,這個 C 代碼的等價物是什么:
char* data = new char[size];
//read file to this array
short version = 0;
memcpy(&version, data, 2);
我試過這個:
//java equivalent of the kotlin code below
Integer.parseInt(String(Arrays.copyOfRange(data, 0, 2)))
//kotlin:
data.copyOfRange(0, 2).concatToString().toInt()
但它不起作用。
ByteBuffer 也不能正常作業:
data[0] is 14
data[1] is 0
ByteBuffer.wrap(data).getShort() is 3584, memcpy is 14
data[2] is -28
data[3] is 45
data[4] is 0
data[5] is 0
ByteBuffer.wrap(data).getInt(2) is -466812928, memcpy is 11748
uj5u.com熱心網友回復:
嘗試使用ByteBuffer:
byte[] data = new byte[size];
ByteBuffer buffer = ByteBuffer.wrap(data);
short version = buffer.getShort();
如果需要,您還可以更改位元組順序
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442161.html
上一篇:嘗試在向量中使用成員類時出錯
下一篇:回傳型別是模板的模板
