import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class BytesToFloat {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//float 类型值为123.456 以大端模式存储数据即高字节存于低地址,低字节存于高地址
byte bytes[]={0x42,(byte) 0xf6,(byte)0xE9,0x79};
//ByteArrayInputStream,使用 buf 作为其缓冲区数组
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
DataInputStream dis=new DataInputStream(new ByteArrayInputStream(b));
float f=dis.readFloat();
dis.close();
System.out.println(f);
}
}
本文介绍了一个Java程序,演示了如何将包含特定字节序列的字节数组转换为浮点数。通过使用ByteArrayInputStream和DataInputStream,程序能够从字节数组中读取并解析出大端模式存储的浮点数值。
1万+

被折叠的 条评论
为什么被折叠?



