0

我知道的关于 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();

    }
}); 
4

1 回答 1

0

利用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

于 2013-07-30T10:16:09.467 回答