简体   繁体   English

如何将InputStream.read()转换为double?

[英]How to convert InputStream.read() to double?

Is there a simple way to convert arrays of bytes read from stream to a double values? 是否有一种简单的方法将从流读取的字节数组转换为双精度值?

InputStream input = serialPort.getInputStream();
while(true) {
  byte[] buffer = new byte[ 8 ];
  len = input.read( buffer );
}

buffer => double ? 缓冲=>双倍?

Can you explain me figuratively how InputStream.read(byte[] b) works? 您能以形象的方式解释我InputStream.read(byte [] b)的工作方式吗?

If you do such operations a lot, maybe it is better to use DataInputStream , which provides a set of operations, like readDouble and so on. 如果您经常进行此类操作,则最好使用DataInputStream ,它提供了一组操作,如readDouble等。

DataInputStream input = new DataInputStream(serialPort.getInputStream());
double d = input.readDouble();

InputStream.read(byte[] b) will read the next bytes into b argument. InputStream.read(byte[] b)将接下来的字节读入b参数。

And, you can try using DataInputStream to read Double, Float, Integer, and others. 并且,您可以尝试使用DataInputStream读取Double,Float,Integer等。 You can instantiate a DataInputStream using: 您可以使用以下方法实例化DataInputStream:

DataInputStream input = new DataInputStream(YOUR_INPUT_STREAM);

尝试使用DataInputStream( javadoc )包装InputStream对象,并使用readDouble方法

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

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