javascript - JS 从缓冲区中读取字节
问题描述
我可以通过一些String
破解来解析缓冲区,但必须有一种更直接的方法来从缓冲区数据到最终值。我一直在尝试使用一些buffer.readInt8
,readInt16LE
等等,但还没有运气。第一个和最后一个值在 2 个字节上;如果我理解这里涉及的词汇。其他二对一。
从下面的例子中,我希望得到温度(228)parseInt(buffer.readUInt16BE(6) | (buffer.readUInt16BE(7) << 8), 16)
。但这给出了345314198
,轻轻地表明我错过了一些东西。
代码
// (example data in comments)
const buffer = peripheral.advertisement.serviceData[0].data;
// a4c1380a701400e4233c0ac5c2
const lameParsing = {
// Starts with the address a4:c1:38:0a:70:14, then the values
temperatureC: parseInt(buffer.toString("hex").slice(12, 16), 16) / 10,
// 22.8
humidity: parseInt(buffer.toString("hex").slice(16, 18), 16),
// 35
battery: parseInt(buffer.toString("hex").slice(18, 20), 16),
// 60
batteryV: parseInt(buffer.toString("hex").slice(20, 24), 16) / 1000
// 2.757
};
语境
解决方案
这应该是您正在寻找的:
let Buf = Buffer.from("a4c1380a701400e4233c0ac5c2", "hex");
// Let Buf be the buffer from the Bluetooth thermometer.
// Sample data is used here, which matches in your problem.
let TemperatureC = Buf.readUInt16BE(6) / 10
let Humidity = Buf.readUIntBE(8,1)
let Battery = Buf.readUIntBE(9,1)
let BatteryV = (Buf.readUInt16BE(10)) / 1000
// Just to confirm it works...
console.log(TemperatureC,Humidity,Battery,BatteryV)
// Sample output: 22.8 35 60 2.757 (Correct)
每个字节都1
在偏移量上。所以,如果我们读取6
2 个字节然后7
,我们实际上是从温度读取第二个字节。记住要考虑到 16 位是 2 个字节;和 NodeJS 按字节偏移。
推荐阅读
- node.js - 从 npm 迁移到 Yarn 2 PnP:ts-loader 找不到 webpack
- javascript - 使用 JavaScript 获取 SharePoint 2013 库中每个视频的 URL
- python - 在一行上执行字符串的大代码片段
- swiftui - 在 SwiftUI 中从 UIViewController 调用函数
- c# - 从外部更改应用程序字符串c#
- python - 如何使用 tkinter 在标签中显示使用 .sum() 的变量的内容
- android - NotificationCompat.Builder setVibrate 在 Android 上根本不振动
- amazon-web-services - 从另一个账户使用 CDK 查询主账户中的 AWS 或 OU?
- c++ - 在 protobuf 中表示 std::optional 数组的最佳方法
- c++ - boost 几何是否支持弯曲几何?