简体   繁体   English

分析字节数组以在android或java中分类

[英]analysis byte array to class in android or java

The only method i known about ByteArrayInputStream is read() but it just return a integer. 我知道的关于ByteArrayInputStream的唯一方法是read(),但它只返回一个整数。

I have a class, and new a variable in someplace. 我有一个类,并且在某个地方新增了一个变量。 then i store the function's return value in devCfgBytes, How to read it as int short or byte to assign it to the correspond int variable or something else. 然后我将函数的返回值存储在devCfgBytes中,如何将其作为int short或byte读取以将其分配给对应的int变量或其他内容。 What should i do? 我该怎么办? any help is appreciated. 任何帮助表示赞赏。

public class ScrewCfg {
    short u16size;
    int u32crc; 
    boolean bEnable;
    byte u8RstFreqDiv;
    final static short screwCfgLegth = 438;
    //...
}
//new obj in someplace
ScrewCfg devCfg = new ScrewCfg();
//....
btnLoadConfig.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        byte[] devCfgBytes = new byte[ScrewCfg.screwCfgLegth];

        int ret = mcLib.ReadUserData(devCfgBytes, ScrewCfg.screwCfgLegth);
        if(ret < 0){
            showMessage("Load Screw configuration Error!");
            return;
        }

        //any method like this? 
        //if i want to read it as boolean, then use one byte
        //read it as integer, then use the four bytes
        ByteArrayInputStream in = new ByteArrayInputStream(devCfgBytes, 1, 1);
        DevCfg.bEnable = in.getBoolean(); 
        in = new ByteArrayInputStream(devCfgBytes, 2, 4);
        DevCfg.u32crc = in.getInt();

    }
}); 

Use DataInputStream 使用DataInputStream

like 喜欢

in = new DataInputStream(new BufferedInputStream(ByteArrayInputStream(devCfgBytes, 1, 1)));
int anInt = in.readInt();
short aShort = in.readShort();
boolean aBool = in.readBoolean();

then you can use readInt , readShort etc. 那么您可以使用readIntreadShort等。

http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM