【发布时间】:2019-09-09 10:35:57
【问题描述】:
伙计们,我有一个问题。
我有一个应用程序,我在其中使用 BLE 从外部设备注册数据。
我有一个“时间”和一个用于“加速度”的数组。
const time = parseInt(
Buffer.from(characteristic.value, "base64")
.readUInt16LE(0)
.toString(16),
16
);
const acc_dx = [2, 4, 6].map(index => {
const hex = Buffer.from(characteristic.value, "base64")
.readInt16LE(index)
.toString(16);
return Number.parseInt(hex, 16);
});
const accUpdate_acc_dx = [...this.state.array_acc_dx, this.state.time, this.state.acc_dx]
this.setState({ array_acc_dx: accUpdate_acc_dx })
array_acc_dx 的结果如下:
[1520,[42,-419,-926],1520,[41,-420,-927],1520,[41,-421,-927],1520,[41,-421,-926 ],1580,[40,-420,-927],1640,[40,-420,-926],1640,[41,-420,-926],1640,[41,-419,-926]
我会得到这个:
1520: [42,-419,-926],
1520: [41,-420,-927],
1520: [41,-421,-927],
1580: [40,-420,-927],
我该怎么做才能拥有这种数组?
【问题讨论】:
-
离题:
parseInt( someNumber.toString(16), 16);的意义何在? -
@Thomas 将数字转换为字符串
-
@Jack23 但
someNumber.toString(16)会将十六进制数字作为字符串返回...然后执行parseInt(str, 16)将再次将该十六进制值转换为数字。如果readUInt16LE返回一个数字,那么您不需要双重转换即可获得相同的数字。如果没有,我认为不需要双重转换。 -
@VLAZ 现在我正在尝试你所说的:)
-
according to the docs,
readInt16LE返回一个与MDN Number type相关的整数。