-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
56 lines (48 loc) · 1.96 KB
/
example.js
File metadata and controls
56 lines (48 loc) · 1.96 KB
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
var DecoderRing = require("decoder-ring");
var decoderRing = new DecoderRing();
var bufferBE = Buffer.alloc(51);
bufferBE.writeInt8(-127, 0);
bufferBE.writeUInt8(254, 1);
bufferBE.writeInt16BE(5327, 2);
bufferBE.writeUInt16BE(5328, 4);
bufferBE.writeFloatBE(-15.33, 6);
bufferBE.writeDoubleBE(-1534.98, 10);
bufferBE.write("ascii", 18, 10,'ascii');
bufferBE.write("utf8 text", 28, 9, 'utf8');
bufferBE.writeUInt8(129, 37);
bufferBE.writeUInt32BE(79001, 38);
bufferBE.writeInt32BE(-79001, 42);
bufferBE.writeInt8(1, 46);
var testBuffer = new Buffer("test");
testBuffer.copy(bufferBE, 47, 0, 4);
var spec = {
bigEndian: true,
fields: [
{ name: "field1", start: 0, type: 'int8' },
{ name: "field2", start: 1, type: 'uint8' },
{ name: "field3", start: 2, type: 'int16' },
{ name: "field4", start: 4, type: 'uint16'},
{ name: "field5", start: 6, type: 'float' },
{ name: "field6", start: 10, type: 'double'},
{ name: "field7", start: 18, type: 'ascii', length: 10 },
{ name: "field8", start: 28, type: 'utf8', length: 9 },
{ name: "field9", start: 37, type: 'bit', position: 7 },
{ name: "field10", start: 37, type: 'bit', position: 6 },
{ name: "field11", start: 37, type: 'bit', position: 0 },
{ name: "field12", start: 38, type: 'uint32' },
{ name: "field13", start: 42, type: 'int32' },
{ name: "field14", start: 46, type: 'int8', default: 42 },
{ name: "field15", start: 47, type: 'buffer', length: 4 }
]
};
// Decode the buffer into a javascript object
var result = decoderRing.decode(bufferBE, spec);
console.log(result);
// Assign field14 to undefined to test default value on encoding
result.field14 = undefined;
// Encode the object to a buffer
var buffer = decoderRing.encode(result, spec);
console.log(buffer);
// Decode buffer to object and check field14 for default value
var resultWithDefaultValue = decoderRing.decode(buffer, spec);
console.log("Field14 default value: " + resultWithDefaultValue.field14);