![](/img/trans.png)
[英]Android/Java Class cast exception.Converting a string to byte array
[英]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();
那么您可以使用readInt
, readShort
等。
http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.