-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (73 loc) · 2.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//ATP util
function isArrayNotEmpty(array) {
return Array.isArray(array) && array.length > 0;
};
function convert16bitesArrayTo(array, output_type) {
if (!isArrayNotEmpty(array))
return;
if (!output_type)
return array[0];
let ret = array[0];
switch (output_type) {
case "uint":
case "uint16":
if (array.length === 1)
ret = (array[0] & 0xFFFF);
break;
case "int":
case "int16":
if (array.length === 1) {
ret = (array[0] & 0xFFFF);
if (array[0] & (1 << 15))
ret |= 0xFFFF0000;
}
break;
case "int":
case "int32":
if (array.length === 2) {
ret = (((array[1] & 0xFFFF) << 16) | (array[0] & 0xFFFF));
if (array[1] & (1 << 15))
ret |= 0xFFFFFFFF00000000;
}
break;
//TODO
// case "uint32":
// if (array.length === 2) {
// ret = (((array[1] & 0xFFFF) << 16) | (array[0] & 0xFFFF));
// ret = Number.toUint32(ret);
// }
// break;
case "string":
case "string8bit":
if (array.length > 0) {
const tmp_stringArray = [];
for (let item of array) {
const char1 = item & 0x00FF;
const char2 = (item & 0xFF00) >> 8;
if (char1 === 0) break;
tmp_stringArray.push(String.fromCharCode(char1))
if (char2 === 0) break;
tmp_stringArray.push(String.fromCharCode(char2))
}
ret = tmp_stringArray.join("");
}
break;
case "string16bit":
if (array.length > 0) {
const tmp_stringArray = [];
for (let item of array) {
if (item === 0) break;
tmp_stringArray.push(String.fromCharCode(item))
}
ret = tmp_stringArray.join("");
}
break;
//TODO: add support for float
default:
}
return ret;
};
module.exports = {
isArrayNotEmpty,
convert16bitesArrayTo
}