错误:无效的hexstring

这是我的节点js代码

if (protocol == '01') { console.log('...goint to get Ack Obj...'); var o = getAckObj(hexString); console.log('...ack obj received...'); var msg = ackMsg(o); console.log('..going to write buffer...'); socket.write(new Buffer(msg, 'hex')); //, 'binary'); console.log('Server sent welcome: ' + msg); } ..... function ackMsg(dataObj) { var ackText = ''; dataObj.len = '05'; //for ack msg its always 05 var e = crc16(dataObj.len + dataObj.protocol + dataObj.serial, 'hex'); dataObj.error = e.toString(16); return dataObj.start + dataObj.len + dataObj.protocol + dataObj.serial + dataObj.error + dataObj.stop; } 

这是hexString 78780d010387113120864842000ccbe40d0a的值

在控制台输出

 ...goint to get Ack Obj... ...ack obj received... ..going to write buffer... buffer.js:348 ret = this.parent.hexWrite(string, this.offset + offset, length); 

你确定你的string的长度是甚至? 当您提供的hexstring是奇数( len % 2 != 0 )而不是所需的偶数时,缓冲区会引发(不清楚)错误消息。

一个很好的testing是logging你所拥有的hexstring,然后在Python中进行testing:

 >>> '8'.decode('hex') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode output = binascii.a2b_hex(input) TypeError: Odd-length string 

我已经在GitHub上打开了一个pull请求来修复错误信息,使其更加清晰: https : //github.com/nodejs/node/pull/4877/files