encode: function (val) { var buf = new Buffer(1); var flags = 0x00; //Bajt 1 { flags = flags | (val.QDCount << 6); flags = flags | (val.ANCount << 6); flags = flags | (val.NSCount << 4); flags = flags | (val.ARCount << 2); } // Encode buf.writeUIntBE(flags, 0); return buf; },
(function() { var buf = new Buffer(3); buf.writeUIntLE(0x123456, 0, 3); assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); assert.equal(buf.readUIntLE(0, 3), 0x123456); buf = new Buffer(3); buf.writeUIntBE(0x123456, 0, 3); assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); assert.equal(buf.readUIntBE(0, 3), 0x123456); buf = new Buffer(3); buf.writeIntLE(0x123456, 0, 3); assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]); assert.equal(buf.readIntLE(0, 3), 0x123456); buf = new Buffer(3); buf.writeIntBE(0x123456, 0, 3); assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]); assert.equal(buf.readIntBE(0, 3), 0x123456); buf = new Buffer(3); buf.writeIntLE(-0x123456, 0, 3); assert.deepEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]); assert.equal(buf.readIntLE(0, 3), -0x123456); buf = new Buffer(3); buf.writeIntBE(-0x123456, 0, 3); assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]); assert.equal(buf.readIntBE(0, 3), -0x123456); buf = new Buffer(3); buf.writeIntLE(-0x123400, 0, 3); assert.deepEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]); assert.equal(buf.readIntLE(0, 3), -0x123400); buf = new Buffer(3); buf.writeIntBE(-0x123400, 0, 3); assert.deepEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]); assert.equal(buf.readIntBE(0, 3), -0x123400); buf = new Buffer(3); buf.writeIntLE(-0x120000, 0, 3); assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0xee]); assert.equal(buf.readIntLE(0, 3), -0x120000); buf = new Buffer(3); buf.writeIntBE(-0x120000, 0, 3); assert.deepEqual(buf.toJSON().data, [0xee, 0x00, 0x00]); assert.equal(buf.readIntBE(0, 3), -0x120000); buf = new Buffer(5); buf.writeUIntLE(0x1234567890, 0, 5); assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); assert.equal(buf.readUIntLE(0, 5), 0x1234567890); buf = new Buffer(5); buf.writeUIntBE(0x1234567890, 0, 5); assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); assert.equal(buf.readUIntBE(0, 5), 0x1234567890); buf = new Buffer(5); buf.writeIntLE(0x1234567890, 0, 5); assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]); assert.equal(buf.readIntLE(0, 5), 0x1234567890); buf = new Buffer(5); buf.writeIntBE(0x1234567890, 0, 5); assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]); assert.equal(buf.readIntBE(0, 5), 0x1234567890); buf = new Buffer(5); buf.writeIntLE(-0x1234567890, 0, 5); assert.deepEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]); assert.equal(buf.readIntLE(0, 5), -0x1234567890); buf = new Buffer(5); buf.writeIntBE(-0x1234567890, 0, 5); assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]); assert.equal(buf.readIntBE(0, 5), -0x1234567890); buf = new Buffer(5); buf.writeIntLE(-0x0012000000, 0, 5); assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]); assert.equal(buf.readIntLE(0, 5), -0x0012000000); buf = new Buffer(5); buf.writeIntBE(-0x0012000000, 0, 5); assert.deepEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]); assert.equal(buf.readIntBE(0, 5), -0x0012000000); })();