在node.js Buffer中搜索字节模式

时间:2021-09-02 21:25:31

I have this node.js Buffer.

我有这个node.js缓冲区。

var test_buf = "5E4D802158D002001022201022AB778899A1B2C3";
var buffer_hex = new Buffer(test_buf, "hex");

I want to search for the existence of byte pattern 77 88 99 in buffer_hex. From the tutorial http://www.tutorialspoint.com/nodejs/nodejs_buffers.htm, I cannot find a suitable Buffer function to use. Any suggestions?

我想在buffer_hex中搜索字节模式77 88 99的存在。从教程http://www.tutorialspoint.com/nodejs/nodejs_buffers.htm,我找不到合适的Buffer函数。有什么建议么?

2 个解决方案

#1


8  

You can use buffer_hex.includes() (or .indexOf() if you need the offset) to find a specific value inside of the buffer. .includes() accepts strings, numbers or other buffers:

如果需要偏移量,可以使用buffer_hex.includes()(或.indexOf())来查找缓冲区内的特定值。 .includes()接受字符串,数字或其他缓冲区:

Worth noting that .includes() is available since Node 5.3.0.

值得注意的是.includes()自Node 5.3.0起可用。

console.log(buffer_hex.includes("778899", 0, "hex")); // boolean
console.log(buffer_hex.indexOf("778899", 0, "hex")); // number

Please note that new Buffer() is deprecated since Node 6. You should use Buffer.from() instead for those versions.

请注意,自节点6以来,不推荐使用新的Buffer()。您应该使用Buffer.from()代替这些版本。

#2


3  

You can use .indexOf():

你可以使用.indexOf():

let included = buffer_hex.indexOf('778899', 0, 'hex') !== -1;

#1


8  

You can use buffer_hex.includes() (or .indexOf() if you need the offset) to find a specific value inside of the buffer. .includes() accepts strings, numbers or other buffers:

如果需要偏移量,可以使用buffer_hex.includes()(或.indexOf())来查找缓冲区内的特定值。 .includes()接受字符串,数字或其他缓冲区:

Worth noting that .includes() is available since Node 5.3.0.

值得注意的是.includes()自Node 5.3.0起可用。

console.log(buffer_hex.includes("778899", 0, "hex")); // boolean
console.log(buffer_hex.indexOf("778899", 0, "hex")); // number

Please note that new Buffer() is deprecated since Node 6. You should use Buffer.from() instead for those versions.

请注意,自节点6以来,不推荐使用新的Buffer()。您应该使用Buffer.from()代替这些版本。

#2


3  

You can use .indexOf():

你可以使用.indexOf():

let included = buffer_hex.indexOf('778899', 0, 'hex') !== -1;