2 回答

TA貢獻1844條經驗 獲得超8個贊
ByteBuffer默認情況下具有大端字節順序,但您的 int 使用小端字節順序。您必須明確地將 ByteBuffer 轉換為小端,例如:
byte[] input = { 45, 0, 0, 0 };
ByteBuffer bb = ByteBuffer.wrap(input).order(ByteOrder.LITTLE_ENDIAN); // <---
int length = bb.getInt();
System.out.println(length); // 45

TA貢獻1812條經驗 獲得超5個贊
你可以這樣做(Java):
String input = "45 0 0 0 65 59 78 76 89 89 78 67 56 67 78 89 98 56 67 78 89 90 98 56 67 78 89 90 56 67 78 89 90 56 67 78 89 90 56 67 78 89 90 56 67 78 89 56 67";
String[] arr = input.split(" ");
int i = 0;
int len = Integer.parseInt(arr[i++]) +
256 * (Integer.parseInt(arr[i++]) +
256 * (Integer.parseInt(arr[i++]) +
256 * Integer.parseInt(arr[i++])));
char[] buf = new char[len];
for (int j = 0; j < len; j++)
buf[j] = (char) Integer.parseInt(arr[i++]);
String result = new String(buf);
System.out.println(result);
輸出
A;NLYYNC8CNYb8CNYZb8CNYZ8CNYZ8CNYZ8CNYZ8CNY8C
添加回答
舉報