Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Node.js中逐字节读取二进制文件_Javascript_Node.js - Fatal编程技术网

Javascript 如何在Node.js中逐字节读取二进制文件

Javascript 如何在Node.js中逐字节读取二进制文件,javascript,node.js,Javascript,Node.js,在Node.js中读取部分二进制文件的最佳方法是什么 我希望访问“头”中的特定字节(小于前100个字节)或逐字节读取文件。以下是fs.read()-从fs.open()返回的文件描述符读取前100个字节的示例: 这是另一种选择 const fs = require('fs'); const fileName = 'something.bin' /* Requirements ex: Content in 512 bytes chunks. Send the 512 bytes packet

在Node.js中读取部分二进制文件的最佳方法是什么


我希望访问“头”中的特定字节(小于前100个字节)或逐字节读取文件。

以下是
fs.read()
-从
fs.open()
返回的文件描述符读取前100个字节的示例:

这是另一种选择

const fs = require('fs');

const fileName = 'something.bin'

/*
Requirements ex:
Content in 512 bytes chunks. 
Send the 512 bytes packet as a 1024 char string, where each byte is sent as a 
2 hex digits. 
An "addr" field starts from 0 and tracks the offset of the first byte of the 
packet. 
*/
function chunk(s, maxBytes) {
  //! https://nodejs.org/api/buffer.html#buffer_buf_slice_start_end
  /*
    buf.slice([start[, end]])
    start <integer> Where the new Buffer will start. Default: 0.
    end <integer> Where the new Buffer will end (not inclusive). Default: buf.length.
    Returns: <Buffer>
  */

  let buf = Buffer.from(s);  
  const result = [];
  let readBuffer = true
  let startChunkByte = 0
  let endChunkByte = maxBytes
  while(readBuffer) {
    // First round
    startChunkByte === 0 ? endChunkByte = startChunkByte + maxBytes : ""

    //Handle last chunk
    endChunkByte >= buf.length ? readBuffer = false : ""

    // addr: the position of the first bytes.  raw: the chunk of x bytes
    result.push({"addr":startChunkByte,"raw":buf.slice(startChunkByte, endChunkByte).toString('hex')});

    startChunkByte = endChunkByte
    endChunkByte = startChunkByte + maxBytes
  }
  return result;
}

fs.readFile(fileName, (err, data) => {
  if (err) throw err;
  // Let's choose 512 bytes chunks
  const arrBinChunk =  chunk(data, 512)
  arrBinChunk.forEach(element => console.log(element))

  //Tests - should be able to see 1024, 1024, as per riquerements
  arrBinChunk.forEach(element => console.log(element.raw.length))
});
const fs=require('fs');
常量文件名='something.bin'
/*
要求,例如:
512字节块中的内容。
将512字节数据包作为1024个字符字符串发送,其中每个字节作为
2个十六进制数字。
“addr”字段从0开始,跟踪
小包裹
*/
函数块(个,最大字节){
//! https://nodejs.org/api/buffer.html#buffer_buf_slice_start_end
/*
buf.slice([start[,end]])
从新缓冲区开始的位置开始。默认值:0。
结束新缓冲区将结束的位置(不包括)。默认值:buf.length。
返回:
*/
设buf=Buffer.from(s);
常量结果=[];
让readBuffer=true
让startChunkByte=0
让endChunkByte=maxBytes
while(读缓冲区){
//第一轮
startChunkByte==0?endChunkByte=startChunkByte+maxBytes:“
//处理最后一块
endChunkByte>=buf.length?readBuffer=false:“
//addr:第一个字节的位置。raw:x字节的块
push({“addr”:startChunkByte,“raw”:buf.slice(startChunkByte,endChunkByte).toString('hex')});
startChunkByte=endChunkByte
endChunkByte=startChunkByte+maxBytes
}
返回结果;
}
fs.readFile(文件名,(错误,数据)=>{
如果(错误)抛出错误;
//让我们选择512字节块
const arrBinChunk=chunk(数据,512)
arrBinChunk.forEach(元素=>console.log(元素))
//测试-根据riquerements,应该能够看到10241024个
forEach(element=>console.log(element.raw.length))
});

@AlexMA我编辑了答案,以反映这一变化,谢谢!它是utf8而不是utf-8,如果我只有字节缓冲呢?
const fs = require('fs');

const fileName = 'something.bin'

/*
Requirements ex:
Content in 512 bytes chunks. 
Send the 512 bytes packet as a 1024 char string, where each byte is sent as a 
2 hex digits. 
An "addr" field starts from 0 and tracks the offset of the first byte of the 
packet. 
*/
function chunk(s, maxBytes) {
  //! https://nodejs.org/api/buffer.html#buffer_buf_slice_start_end
  /*
    buf.slice([start[, end]])
    start <integer> Where the new Buffer will start. Default: 0.
    end <integer> Where the new Buffer will end (not inclusive). Default: buf.length.
    Returns: <Buffer>
  */

  let buf = Buffer.from(s);  
  const result = [];
  let readBuffer = true
  let startChunkByte = 0
  let endChunkByte = maxBytes
  while(readBuffer) {
    // First round
    startChunkByte === 0 ? endChunkByte = startChunkByte + maxBytes : ""

    //Handle last chunk
    endChunkByte >= buf.length ? readBuffer = false : ""

    // addr: the position of the first bytes.  raw: the chunk of x bytes
    result.push({"addr":startChunkByte,"raw":buf.slice(startChunkByte, endChunkByte).toString('hex')});

    startChunkByte = endChunkByte
    endChunkByte = startChunkByte + maxBytes
  }
  return result;
}

fs.readFile(fileName, (err, data) => {
  if (err) throw err;
  // Let's choose 512 bytes chunks
  const arrBinChunk =  chunk(data, 512)
  arrBinChunk.forEach(element => console.log(element))

  //Tests - should be able to see 1024, 1024, as per riquerements
  arrBinChunk.forEach(element => console.log(element.raw.length))
});