I am implementing a binary protocol and sending a buffer to a server. One of the fields, Password, is defined as follows in the documentation:
Data Type: Fixed String
Length: 32 bytes
Offset: 58
Description:
Fixed size of 32 bytes.
Encoded as a character array.
Must be completely filled with valid characters (space padding if required).
Valid characters: 0-9, A-Z, a-z, !, #, $, %, &, *, +, -, ., /, =, @, _.
I have attched below images to get Idea of padding len and offset.
Image of Datatype table
Image of field details table
Here’s my code for constructing the buffer:
<code>
const createGatewayRequest = () => {
const buffer = Buffer.alloc(96); // Total message size: 96 bytes
let offset = 0;
// 1. BodyLen: 4 bytes (unsigned int, little-endian)
buffer.writeUInt32LE(96, offset);
offset += 4;
// 2. TemplateID: 2 bytes (unsigned int, little-endian)
buffer.writeUInt16LE(10020, offset);
offset += 2;
// 3. NetworkMsgID: 8 bytes (Fixed String, zero-padded)
buffer.fill(0x00, offset, offset + 8);
offset += 8;
// 4. Pad2: 2 bytes (not used, zero-padded)
buffer.fill(0x00, offset, offset + 2);
offset += 2;
// 5. MsgSeqNum: 4 bytes (unsigned int, little-endian, must be set to 1)
buffer.writeUInt32LE(1, offset);
offset += 4;
// 6. SenderSubID: 4 bytes (not used, set to 0xFFFFFFFF)
buffer.writeUInt32LE(0xFFFFFFFF, offset);
offset += 4;
// 7. PartyIDSessionID: 4 bytes (unsigned int, little-endian)
const partyIDSessionID = 123123123;
buffer.writeUInt32LE(partyIDSessionID, offset);
offset += 4;
// 8. DefaultCstmApplVerID: 30 bytes (ASCII, padded with spaces)
const defaultCstmApplVerID = '2.4';
buffer.write(defaultCstmApplVerID.padEnd(30, ' '), offset, 30, 'ascii');
offset += 30;
// 9. Password: 32 bytes (ASCII, padded with spaces)
const Password = "pwd12345";
buffer.write(Password.padStart(32, ' '), offset, 32, 'ascii');
offset += 32;
buffer.fill(0x00, offset, offset + 6);
offset += 6;
console.log('Gateway Request (hex):', buffer.toString('hex'));
return buffer;
};
</code>
<code>
const createGatewayRequest = () => {
const buffer = Buffer.alloc(96); // Total message size: 96 bytes
let offset = 0;
// 1. BodyLen: 4 bytes (unsigned int, little-endian)
buffer.writeUInt32LE(96, offset);
offset += 4;
// 2. TemplateID: 2 bytes (unsigned int, little-endian)
buffer.writeUInt16LE(10020, offset);
offset += 2;
// 3. NetworkMsgID: 8 bytes (Fixed String, zero-padded)
buffer.fill(0x00, offset, offset + 8);
offset += 8;
// 4. Pad2: 2 bytes (not used, zero-padded)
buffer.fill(0x00, offset, offset + 2);
offset += 2;
// 5. MsgSeqNum: 4 bytes (unsigned int, little-endian, must be set to 1)
buffer.writeUInt32LE(1, offset);
offset += 4;
// 6. SenderSubID: 4 bytes (not used, set to 0xFFFFFFFF)
buffer.writeUInt32LE(0xFFFFFFFF, offset);
offset += 4;
// 7. PartyIDSessionID: 4 bytes (unsigned int, little-endian)
const partyIDSessionID = 123123123;
buffer.writeUInt32LE(partyIDSessionID, offset);
offset += 4;
// 8. DefaultCstmApplVerID: 30 bytes (ASCII, padded with spaces)
const defaultCstmApplVerID = '2.4';
buffer.write(defaultCstmApplVerID.padEnd(30, ' '), offset, 30, 'ascii');
offset += 30;
// 9. Password: 32 bytes (ASCII, padded with spaces)
const Password = "pwd12345";
buffer.write(Password.padStart(32, ' '), offset, 32, 'ascii');
offset += 32;
buffer.fill(0x00, offset, offset + 6);
offset += 6;
console.log('Gateway Request (hex):', buffer.toString('hex'));
return buffer;
};
</code>
const createGatewayRequest = () => {
const buffer = Buffer.alloc(96); // Total message size: 96 bytes
let offset = 0;
// 1. BodyLen: 4 bytes (unsigned int, little-endian)
buffer.writeUInt32LE(96, offset);
offset += 4;
// 2. TemplateID: 2 bytes (unsigned int, little-endian)
buffer.writeUInt16LE(10020, offset);
offset += 2;
// 3. NetworkMsgID: 8 bytes (Fixed String, zero-padded)
buffer.fill(0x00, offset, offset + 8);
offset += 8;
// 4. Pad2: 2 bytes (not used, zero-padded)
buffer.fill(0x00, offset, offset + 2);
offset += 2;
// 5. MsgSeqNum: 4 bytes (unsigned int, little-endian, must be set to 1)
buffer.writeUInt32LE(1, offset);
offset += 4;
// 6. SenderSubID: 4 bytes (not used, set to 0xFFFFFFFF)
buffer.writeUInt32LE(0xFFFFFFFF, offset);
offset += 4;
// 7. PartyIDSessionID: 4 bytes (unsigned int, little-endian)
const partyIDSessionID = 123123123;
buffer.writeUInt32LE(partyIDSessionID, offset);
offset += 4;
// 8. DefaultCstmApplVerID: 30 bytes (ASCII, padded with spaces)
const defaultCstmApplVerID = '2.4';
buffer.write(defaultCstmApplVerID.padEnd(30, ' '), offset, 30, 'ascii');
offset += 30;
// 9. Password: 32 bytes (ASCII, padded with spaces)
const Password = "pwd12345";
buffer.write(Password.padStart(32, ' '), offset, 32, 'ascii');
offset += 32;
buffer.fill(0x00, offset, offset + 6);
offset += 6;
console.log('Gateway Request (hex):', buffer.toString('hex'));
return buffer;
};
1