字节序

本文详细解释了计算机系统中数据存储的两种主要方式:大端模式和小端模式,并通过具体的例子进行说明。此外,还提供了将小端模式的字节数组转换为Short类型的Java代码示例,并展示了如何检查平台的字节顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是大小端?

例如:
90AB12CD
In big endian, you store the most significant byte in the smallest address. Here’s how it would look:

AddressValue
100090
1001AB
100212
1003CD

In little endian, you store the least significant byte in the smallest address. Here’s how it would look:

AddressValue
1000CD
100112
1002AB
100390

从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’.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值