从二进制文件javascript读取浮动

从二进制文件javascript读取浮动,javascript,java,php,file,Javascript,Java,Php,File,我试图从使用Java创建的二进制文件中读取javascript中的浮点值 该文件是使用DataOutputStream在Java中创建的: DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); dos.writeFloat(-222); dos.writeFloat(222000); dos.writeFloat(130.329f); dos.flush

我试图从使用Java创建的二进制文件中读取javascript中的浮点值

该文件是使用DataOutputStream在Java中创建的:

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
dos.writeFloat(-222);
dos.writeFloat(222000);
dos.writeFloat(130.329f);
dos.flush();
dos.close();
该文件通过http请求检索,读取方式如下:

var client = new XMLHttpRequest();
client.addEventListener("load", dataLoaded);
client.open("GET", "/ajax-requests.php?data=true", true);
client.responseType = "arraybuffer";
client.send();
数据加载函数:

function dataLoaded () {
    console.log("Float32Array: " + new Float32Array(this.respnse));
}
输出:

Float32Array: 3.3994099446055737e-41,1.8766110561523948e-38,0.00020218738063704222
dataView: -222
dataView: 222000
dataView: 130.32899475097656
期望:

Float32Array: -222,222000,130.329
该文件通过php发送:

if(isset($_GET['data'])) {
  $file_path = "data/filename.ext";

  if (file_exists($file_path)) {
    if(false !== ($handler = fopen($file_path, 'r'))) {
        header("Content-Type: application/octet-stream");
        header("Content-Length: " . filesize($file_path));

        readfile($file_path);
    }
    exit;
  }
  echo "<h1>Content error</h1><p>The file does not exist!</p>";
}
输出:

Float32Array: 3.3994099446055737e-41,1.8766110561523948e-38,0.00020218738063704222
dataView: -222
dataView: 222000
dataView: 130.32899475097656

您的字节顺序不匹配。DataOutputStream以大端号写入,但Float32Array通常以小端号读取,具体取决于硬件。你必须改变或匹配。

谷歌搜索每个计算机科学家需要知道的关于浮点的知识