使用extendscript读取二进制文件 [英] Read binary file with extendscript

查看:187
本文介绍了使用extendscript读取二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅限于在Photoshop中使用Extendscript,我试图编写然后在同一二进制文件中读取. 我可以写文件,但是我不确定读取部分出了什么问题.

Limited to using Extendscript in Photoshop I'm trying to write and then read in the same binary file. I can write the file okay, but I'm not sure where I'm going wrong with the read part.

数据将是十六进制的RGB颜色,因此我要么想从读取函数中以数组或字符串的形式返回数据.只有我什至无法告诉我刚刚写入的文件存在.而且我不确定是否应该使用seek()或read().糊涂了.

The data will be RGB colours in hex, so I'll either want to return the data from the read function as array or a string. Only I can't even get it to tell me the file just written exists. And I'm not sure if I should be using seek() or read(). Confused.

var f = new File("D:\\temp\\bin.act");

var w = write_binary(f);
var r = read_binary(w); 

alert(r);

function write_binary(afile)
{
  afile.encoding = "BINARY";
  afile.open ("w");

  for(i = 0; i < 256; i++)
  {
    afile.write(String.fromCharCode (i));
  }

  afile.close();
}


function read_binary(afile)
{
  var f = new File(afile);
  f.open("r");
  f.encoding = "BINARY";

  //var data = f.read();
  //if(f.exists) alert(afile);
  //alert (data);

  var arr = [];
  for (var i = 0; i < f.length; i+=4)
  {
    f.seek(i, 0);
    var hex = f.readch().charCodeAt(0).toString(16);
    if(hex.length === 1) hex = "0" + hex;
    arr.push(hex);
  }
  return arr;
}

推荐答案

您可以这样阅读:

// Open binary file
var afile = "/Users/mark/StackOverflow/data.bin"
var f = new File(afile);
f.open("r");
f.encoding = "BINARY";
alert('OK');

var hexstring=""
while (true){
    var b = f.readch().charCodeAt(0);
    if(f.eof){break;}
    var hex = b.toString(16);
    if(hex.length === 1) hex = "0" + hex;
    hexstring += hex;
}
alert(hexstring);

此答案的相应写作部分是此处.

The corresponding writing part of this answer is here.

这篇关于使用extendscript读取二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆