var buffer = require('buffer');
var bufferpack = require('bufferpack');
var fmt;
var pack = function(data) {
var header = '000000000000000000xxxxxxxxxxxxxx';
var packlen = data.length;
var packarr = [header, packlen, data];
var fmtStr = 'sis'; //字符串长整型字符串
var fmtStrRes = [];
var idx = 0;
for (k in fmtStr) {
v = fmtStr[k];
if(v=='i') { //signed long
fmtStrRes.push('i');
}else if(v=='b'){ //signed char
fmtStrRes.push('b');
}else if(v=='s'){ //char[]
var _strLength = packarr[idx].length;
fmtStrRes.push(_strLength.toString()+'s');
}
idx += 1;
}
fmt = '<'+fmtStrRes.join('');
console.log('packarr='+packarr);
console.log('fmt='+fmt);
res = bufferpack.pack(fmt, packarr);
console.log('pack=', res);
}
var packed = pack('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
var unpacked = bufferpack.unpack(fmt, packed, 0);
为什么 unpack的时候报错了,求解,应该是没有问题才对啊