EVAL
// pack() simple merge Buffers. // If you want pure Buffer without meta info. // please insert '#' charactor into name filed. or use null string "". prn('\n#','Pure Buffer') let pureBuffer = MBP.pack( MBP.MB('#v1','16',15), // Uint16 MBP.MB('#v2','i16',-15), //Int16 MBP.MB('','32', 0x11223344), // Uint32 BigEndian MBP.MB('','32L', 0x11223344) // Uint32 LittleEndian ) prn(' pureBuffer', MBP.hex( pureBuffer ) ) // No meta info. prn(' pureBuffer.byteLength ', pureBuffer.byteLength ) // 12bytes : 2 + 2 + 4 + 4 prn('\n#','Buffer with Meta Info') prn(' -','added addtional data.') // pack() : multiple meta buffers => one buffer let mbPack = MBP.pack( MBP.MB('v1','16',15), // Uint16 MBP.MB('v2','i16',-15), //Int16 MBP.MB('v3','32', 0x11223344), // Uint32 BigEndian MBP.MB('v4','32L', 0x11223344), // Uint32 LittleEndian MBP.MB('vStr','abcde'), // buffer string ) // hex view let hex = MBP.hex( mbPack ) prn('mbpHex', hex) // unpack(): buffer -> object let mbpObj = MBP.unpack( mbPack ) prnObj('mbpObj', mbpObj) prn( 'mbpObj.v1', mbpObj.v1 ) prn( 'mbpObj.v2', mbpObj.v2 ) // MBA function packFunctionParams( ...args){ return MBP.pack( MBP.MBA(...args) ) } let mbaPack = packFunctionParams( 'hi', 2332, 22.2, [1, 2, 3], { 'hi': 'yeh' }, true ) prn( 'mbaPack.byteLength', mbaPack.byteLength ) let mbaObj = MBP.unpack( mbaPack ) prnObj('mbaObj', mbaObj.args ) // or mbaObj.$ // Node Buffer in the browser. const Buffer = MBP.Buffer; let buf = Buffer.from([1,2,3] ) prn( 'hex', buf.toString('hex') ) prn( 'base64', buf.toString('base64') ) prn( 'hex', Buffer.from('abcdefg').toString('hex') )
result: