简体   繁体   English

node.js 将二进制字符串转换为数字

[英]node.js convert binary string to number

Writing a node.js application, it receives from a socket a binary string of 4 bytes "\\x00\\x00\\x00\\x13", I need to convert it to integer (19 which is 0x13 in our case) using big endian order.编写一个 node.js 应用程序,它从一个套接字接收一个 4 字节的二进制字符串“\\x00\\x00\\x00\\x13”,我需要使用大端顺序将它转换为整数(在我们的例子中是 0x13)。

In other scripting languages, like perl, we can do unpack.在其他脚本语言中,比如 perl,我们可以解包。 unpack("N", )解包(“N”,)

In node.js, not sure how do do it.在 node.js 中,不知道怎么做。

new Buffer('\x00\x00\x00\x13', 'binary').readInt32BE(0)

npm is your friend here. npm 是你的朋友。 The module anybase looks like it might be up to the task.模块anybase看起来可以完成任务。 Here's a link for more info: https://www.npmjs.org/package/anybase这是更多信息的链接: https : //www.npmjs.org/package/anybase

Future tip:未来提示:
you can search npm from the commandline and probably find a module for most common things.您可以从命令行搜索 npm,并且可能会为最常见的东西找到一个模块。
The syntax is like this:语法是这样的:

npm search string1 string2 string3

And it will match any module that has all the strings listed.它将匹配列出了所有字符串的任何模块。 This following command led me to anybase:以下命令将我引导至 anybase:

npm search binary convert

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM