什么是大小端?
例如:
90AB12CD
In big endian, you store the most significant byte in the smallest address. Here’s how it would look:
Address | Value |
---|---|
1000 | 90 |
1001 | AB |
1002 | 12 |
1003 | CD |
In little endian, you store the least significant byte in the smallest address. Here’s how it would look:
Address | Value |
---|---|
1000 | CD |
1001 | 12 |
1002 | AB |
1003 | 90 |
从Audio Recorder里读出来的byte array 数据是小端的, 可以用以下的代码转成Short
public static short[] bytesToShorts(byte[] b) {
short[] s = new short[b.length / 2];
for (int i = 0; i < s.length; i++) {
s[i] = (short) ((b[i * 2] & 0xff) | ((b[i * 2 + 1] << 8) & 0xff00));
}
return s;
}
AudioRecord还可以直接读short
int read (short[] audioData, int offsetInShorts, int sizeInShorts)
write code to check the platform endian:
Suppose we are on a 32-bit machine.
If it is little endian, the x in the memory will be something like:
higher memory
----->
+----+----+----+----+
|0x01|0x00|0x00|0x00|
+----+----+----+----+
A
|
&x
so (char*)(*x) == 1
If it is big endian, it will be:
+----+----+----+----+
|0x00|0x00|0x00|0x01|
+----+----+----+----+
A
|
&x
so this one will be ‘0’.