ContextHub: java 字节和数据类型的转换

背景

因java的默认字节序是大端,而slp dsp需要的是小端,所以需要转换,否则会出现各种问题。

测试用例
ByteBuffer buf = ByteBuffer.allocate(4);
Log.i(TAG, "Default java endian: "+buf.order().toString());
buf.putInt(0x2);
byte[] result = buf.array();
Log.i(TAG, "bytes: " + result[0]);
printBytes(result);


ByteBuffer buf1 = ByteBuffer.allocate(4);
buf1.order(ByteOrder.LITTLE_ENDIAN);
buf1.putInt(0x2);
Log.i(TAG, "now java endian: "+buf1.order().toString());
result = buf1.array();
Log.i(TAG, "bytes: " + result[0]);
printBytes(result);

01-01 01:15:44.734  8954  8954 I little_big_edian: Default java endian: BIG_ENDIAN
01-01 01:15:44.734  8954  8954 I little_big_edian: bytes: 0
01-01 01:15:44.734  8954  8954 I little_big_edian: 0 0 0 2 


01-01 01:15:44.734  8954  8954 I little_big_edian: now java endian: LITTLE_ENDIAN
01-01 01:15:44.734  8954  8954 I little_big_edian: bytes: 2
01-01 01:15:44.734  8954  8954 I little_big_edian: 2 0 0 0 
 

幸运的是native写的代码可以运行,而java层失败,把数据打印出就可发现问题

01-01 01:52:10.271  9324  9324 E CHRE    : input parameter length is: 80 bytes
01-01 01:52:10.271  9324  9324 D CHRE    : Delivering message from host (size 80)
01-01 01:52:10.271  9324  9324 D CHRE    : Dumping buffer of size 80 bytes
01-01 01:52:10.271  9324  9324 D CHRE    :   14 00 00 00  00 00 00 00     ........
01-01 01:52:10.271  9324  9324 D CHRE    :   00 00 0a 00  10 00 07 00     ........
01-01 01:52:10.271  9324  9324 D CHRE    :   08 00 0e 00  0a 00 00 00     ........
01-01 01:52:10.271  9324  9324 D CHRE    :   00 00 00 01  14 00 00 00     ........
01-01 01:52:10.271  9324  9324 D CHRE    :   00 00 01 00  0c 00 18 00     ........
01-01 01:52:10.271  9324  9324 D CHRE    :   10 00 08 00  06 00 0c 00     ........
01-01 01:52:10.271  9324  9324 D CHRE    :   0c 00 00 00  00 00 02 00     ........
01-01 01:52:10.271  9324  9324 D CHRE    :   05 04 00 00  0c 00 00 00     ........
01-01 01:52:10.271  9324  9324 D CHRE    :   14 00 00 4d  4f 43 51 20     ...MOCQ 
01-01 01:52:10.271  9324  9324 D CHRE    :   04 00 00 00  00 00 00 02     ........
01-01 01:52:10.313  9324  9324 E CHRE    : Failed to deliver message from host to CHRE: 104

何为大小端

大小端的意思就是如果是占用多个字节的数据类型,数据的低位存储在地址的低位还是高位,如果是低位就是little endian

反之,则是big endian

frameworks/base/core/java/android/hardware/location/NanoAppBinary.java

/*
* Byte order established in context_hub.h
*/
private static final ByteOrder HEADER_ORDER = ByteOrder.LITTLE_ENDIAN;

已知byte[]到具体数据类型的转换

源码给出了一个例子

frameworks/base/core/java/android/hardware/location/NanoAppBinary.java

public NanoAppBinary(byte[] appBinary) {
        mNanoAppBinary = appBinary;
        parseBinaryHeader();
}

/*
* Byte order established in context_hub.h
*/
private static final ByteOrder HEADER_ORDER = ByteOrder.LITTLE_ENDIAN;

    private void parseBinaryHeader() {

       //由byte[]创建ByteBuffer,并设置大小端
        ByteBuffer buf = ByteBuffer.wrap(mNanoAppBinary).order(HEADER_ORDER);

        mHasValidHeader = false;

       //由ByteBuffer的成员函数getInt/Long/Float等依次获得数据,指针默认从header开始
        try {
            mHeaderVersion = buf.getInt();
            if (mHeaderVersion != EXPECTED_HEADER_VERSION) {
                Log.e(TAG, "Unexpected header version " + mHeaderVersion + " while parsing header"
                        + " (expected " + EXPECTED_HEADER_VERSION + ")");
                return;
            }

            mMagic = buf.getInt();
            mNanoAppId = buf.getLong();
            mNanoAppVersion = buf.getInt();
            mFlags = buf.getInt();
            mHwHubType = buf.getLong();
            mTargetChreApiMajorVersion = buf.get();
            mTargetChreApiMinorVersion = buf.get();
        }

基本数据类型数据转换为byte[]

 //根据数据类型的大小创建ByteBuffer,并设定ByteOrder

ByteBuffer buf = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
// 使用get对应的函数put赋值ByteBuffer
buf.putInt(0x02);
//得到byte[]
buf.array()//byte[]

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值