Javascript 从二进制文件读取浮点值(在after effects脚本中)

Javascript 从二进制文件读取浮点值(在after effects脚本中),javascript,binary,floating-point,extendscript,after-effects,Javascript,Binary,Floating Point,Extendscript,After Effects,我有一个二进制文件,其中包含使用c程序记录的数据。 文件中存储的数据是浮点值。 现在我需要从after effects脚本中的二进制文件中检索浮点数。 这是我的代码: var myFile = File.openDialog('select file'); myFile.open("r"); myFile.encoding = "binary"; for(x=0;x<myFile.length;x += 4){ myFile.seek(x,0); buffer = my

我有一个二进制文件,其中包含使用c程序记录的数据。 文件中存储的数据是浮点值。 现在我需要从after effects脚本中的二进制文件中检索浮点数。 这是我的代码:

var myFile = File.openDialog('select file');
myFile.open("r");
myFile.encoding = "binary";
for(x=0;x<myFile.length;x += 4){
     myFile.seek(x,0);
     buffer = myFile.read(4);
     ???
}
var myFile=File.openDialog('select File');
myFile.open(“r”);
myFile.encoding=“binary”;

对于(x=0;x试试这个。它假设输入是由编写的。我使用了.Sample input file is的解析函数。它由4个值组成(7.26,-3.32,-5.18,7.66),没有分隔符,因此它的大小是4*4=16字节

var myFile = File.openDialog('select file');
myFile.open("r");
myFile.encoding = "binary";

var buffers = [];
for(var x=0; x<myFile.length; x += 4) {
    myFile.seek(x, 0);
    var buffer = "0x";
    for(var y=0; y<4; y++) {
        var hex = myFile.readch().charCodeAt(0).toString(16);
        if(hex.length === 1) {
            hex = "0" + hex;
        }
        buffer += hex;
    }
    buffers.push(parseFloat2(buffer));

}
alert(buffers);


function parseFloat2(str) {
    // from https://stackoverflow.com/a/14090278/6153990
    var float2 = 0;
    var sign, order, mantiss, exp, int2 = 0, multi = 1;
    if (/^0x/.exec(str)) {
        int2 = parseInt(str,16);
    } else {
        for (var i = str.length -1; i >=0; i -= 1) {
            if (str.charCodeAt(i)>255) {
                alert('Wrong string parametr'); 
                return false;
            }
            int2 += str.charCodeAt(i) * multi;
            multi *= 256;
        }
    }
    sign = (int2>>>31)?-1:1;
    exp = (int2 >>> 23 & 0xff) - 127;
    mantiss = ((int2 & 0x7fffff) + 0x800000).toString(2);
    for (i=0; i<mantiss.length; i+=1){
        float2 += parseInt(mantiss[i])? Math.pow(2,exp):0;
        exp--;
    }
    return float2*sign;
}
var myFile=File.openDialog('select File');
myFile.open(“r”);
myFile.encoding=“binary”;
var缓冲区=[];
对于(变量x=0;x255){
警报(“错误的字符串参数”);
返回false;
}
int2+=str.charCodeAt(i)*multi;
多重*=256;
}
}
符号=(int2>>>31)?-1:1;
exp=(int2>>>23&0xff)-127;
mantiss=(int2&0x7fffff)+0x800000.toString(2);

对于(i=0;我猜这是extendscript?您能简单解释一下myFile.read(4)是什么吗将缓冲区设置为?它是一个0和1的字符串,还是一个实际的二进制数?可能会对您有所帮助,但我现在找不到官方API文档…是的,它是extendscript。如果您发布输入文件并编写一些所需的输出示例,您将获得更多帮助。非常感谢Sangbok,我为第一次邮递添加了一些详细信息非常感谢Sangbok Lee!您的示例文件已丢失,我无法测试。但我会尽快编写一个新的示例文件并进行测试。+您能重新加载您的示例文件吗?是的。