-
Notifications
You must be signed in to change notification settings - Fork 7
/
playWithBins.js
145 lines (143 loc) · 2.99 KB
/
playWithBins.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Play with converting an array of values to better human readable format using the ASCII chart
const parse = (value) => {
console.log(value);
let ascii = "",
bin = "";
for (let i = 0; i < value.byteLength; i++) {
bin += `${value.getUint8(i)} `;
asciiF = () => {
let v;
switch (value.getUint8(i)) {
case 0xa0:
v = "+ ";
break;
case 0x00:
v = "<NUL> ";
break;
case 0x01:
v = "<SOH> ";
break;
case 0x02:
v = "<STX> ";
break;
case 0x03:
v = "<ETX> ";
break;
case 0x04:
v = "<EOT> ";
break;
case 0x05:
v = "<ENQ> ";
break;
case 0x06:
v = "<ACK> ";
break;
case 0x07:
v = "<BEL> ";
break;
case 0x08:
v = "<BS> ";
break;
case 0x09:
v = "<HT> ";
break;
case 0x0a:
v = "<LF/NL> ";
break;
case 0x0b:
v = "<VT> ";
break;
case 0x0c:
v = "<FF> ";
break;
case 0x0d:
v = "<CR> ";
break;
case 0x0e:
v = "<SO> ";
break;
case 0x0f:
v = "<SI> ";
break;
case 0x10:
v = "<DLE> ";
break;
case 0x11:
v = "<DC1> ";
break;
case 0x12:
v = "<DC2> ";
break;
case 0x13:
v = "<DC3> ";
break;
case 0x14:
v = "<DC4> ";
break;
case 0x15:
v = "<NAK> ";
break;
case 0x16:
v = "<SYN> ";
break;
case 0x17:
v = "<ETB> ";
break;
case 0x18:
v = "<CAN> ";
break;
case 0x19:
v = "<EM> ";
break;
case 0x1a:
v = "<SUB> ";
break;
case 0x1b:
v = "<ESC> ";
break;
case 0x1c:
v = "<FS> ";
break;
case 0x1d:
v = "<GS> ";
break;
case 0x1e:
v = "<RS> ";
break;
case 0x1f:
v = "<US> ";
break;
default:
v = `${String.fromCharCode(value.getUint8(i))} `;
break;
}
return v;
};
ascii += asciiF();
}
return [bin, ascii];
};
let toWrtieArray = [
'0x01',
'h',
'1',
1
];
let buffer = new ArrayBuffer(toWrtieArray.length);
let view = new DataView(buffer, 0);
let v
for (let i = 0; i < toWrtieArray.length; i++) {
if ( typeof toWrtieArray[i] === 'string' && toWrtieArray[i].charAt(1) === 'x' ) {
let hexString = toWrtieArray[i]
let hex = hexString.replace(/^0x/, '');
v = hex.match(/[\dA-F]{2}/gi);
}
else if ( typeof toWrtieArray[i] === 'number' ) {
v = toWrtieArray[i]
}
else {
v = new TextEncoder().encode(toWrtieArray[i])
}
view.setUint8(i,v)
}
console.log(parse(view));