繁体   English   中英

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

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

我知道的关于ByteArrayInputStream的唯一方法是read(),但它只返回一个整数。

我有一个类,并且在某个地方新增了一个变量。 然后我将函数的返回值存储在devCfgBytes中,如何将其作为int short或byte读取以将其分配给对应的int变量或其他内容。 我该怎么办? 任何帮助表示赞赏。

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();

    }
}); 

使用DataInputStream

喜欢

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

那么您可以使用readIntreadShort等。

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