我正在读这样的二进制文件:
InputStream in = new FileInputStream( file );
byte[] buffer = new byte[1024];
while( ( in.read(buffer ) > -1 ) {
int a = // ???
}
Run Code Online (Sandbox Code Playgroud)
我想做什么来读取最多4个字节并从那些创建一个int值但是,我不知道该怎么做.
我觉得我必须一次抓取4个字节,并执行一个"字节"操作(如>> << >>&FF和类似的东西)来创建新的int
这个成语是什么?
编辑
哎呀,结果有点复杂(解释)
我想要做的是,读取一个文件(可能是ascii,二进制,无所谓)并提取它可能具有的整数.
例如,假设二进制内容(在基数2中):
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000010
Run Code Online (Sandbox Code Playgroud)
整数表示应该是1,2对吗?: - 前1位为32位,其余32位为2位.
11111111 11111111 11111111 11111111
Run Code Online (Sandbox Code Playgroud)
将是-1
和
01111111 11111111 11111111 11111111
Run Code Online (Sandbox Code Playgroud)
将会 Integer.MAX_VALUE ( 2147483647 )
我正在开发一个使用RXTX和protobuf与开发板上的应用程序进行通信的项目,而且我遇到了一些问题,这些问题意味着我可能会以错误的方式做事.这是我目前用于向板上写请求的内容(读取代码类似):
public void write(CableCommandRequest request, OutputStream out) {
CodedOutputStream outStream = CodedOutputStream.newInstance(out);
request.writeTo(outStreatm);
outStream.flush();
}
Run Code Online (Sandbox Code Playgroud)
以下是用于准备RXTX串行连接的设置,该连接又支持命令OutputStream使用的设置write:
// The baud rate to use when connecting to the development board
private final static int BAUD_RATE = 115200;
// The timeout to use for the serial port
private final static int CONNECTION_TIMEOUT = 50;
// The serial break for the development board, 100
private final static int SERIAL_BREAK = 100;
// <SNIP> ...
SerialPort serialPort …Run Code Online (Sandbox Code Playgroud)