douji9518
2017-06-05 16:44
浏览 70
已采纳

从二进制文件javascript中读取浮点数

I'm trying to read floats in javascript from a binary file that is created using Java.

The file is created in Java using DataOutputStream:

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
dos.writeFloat(-222);
dos.writeFloat(222000);
dos.writeFloat(130.329f);
dos.flush();
dos.close();

The file is retreived by http request and read like this:

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

dataLoaded function:

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

Output:

Float32Array: 3.3994099446055737e-41,1.8766110561523948e-38,0.00020218738063704222

Expecting:

Float32Array: -222,222000,130.329

The file is sent with 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>";
}

It seems that somewhere there is a flaw in the conversion but I can't figure out where.

UPDATE:

The problem was just as Sean Van Gorder stated, a difference in endianness. To work around this I used DataView to read the arrayBuffer (since the file will be read both in java and in javascript this was the best sulotion)

var dataView = new DataView(arrayBuffer);
console.log("dataView: " + dataView.getFloat32(0, false));
console.log("dataView: " + dataView.getFloat32(4, false));
console.log("dataView: " + dataView.getFloat32(8, false));

Output:

dataView: -222
dataView: 222000
dataView: 130.32899475097656

图片转代码服务由CSDN问答提供 功能建议

我正在尝试从使用Java创建的二进制文件中读取javascript中的浮点数。 \ n

使用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); \  nclient.open(“GET”,“/ ajax-requests.php?data = true”,true); 
client.responseType =“arraybuffer”; 
client.send(); 
    
 
 

dataLoaded函数:

  function dataLoaded(){
 console.log(“Float32Array:”+ new Float32Array(this.respnse)); \  n} 
   
 
 

输出:

  Float32Array:3.3994099446055737e-41,1.8766110561523948e-38,0.00020218738063704222 
    
 
 

预期:

  Float32Array:-222,222000,130.329 
   
 \  n 

该文件使用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“&lt; h1&gt;内容错误&lt; / h1&gt;&lt; p&gt;该文件不存在 !&lt; / p&gt;“; 
} 
   
 
 

似乎转换中有一个缺陷,但我无法弄清楚在哪里。

更新:

问题就像Sean Van Gorder所说的那样,这是一个不同的字节序。 为了解决这个问题,我使用DataView来读取arrayBuffer(因为文件将在java和javascript中读取,这是最好的闷)。

  var dataView = new DataView(  arrayBuffer); 
console.log(“dataView:”+ dataView.getFloat32(0,false)); 
console.log(“dataView:”+ dataView.getFloat32(4,false)); 
console.log(“dataView”  :“+ dataView.getFloat32(8,false)); 
   
 
 

输出:

  dataView:-222  
dataView:222000 
dataView:130.32899475097656 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxiejie9387 2017-06-05 17:58
    已采纳

    You've got a byte order mismatch. DataOutputStream writes in big-endian, but Float32Array usually reads in little-endian (depending on hardware). You'll have to change the Java side or the Javascript side to match.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题