javascript - Node.js: Cannot set a value '0xff' by 'buf.writeIntBE()' -
i can write -1 buffer writeintbe, code is:
var b = new buffer(1); b.writeintbe(-1, 0, 1); console.log(b); //<buffer ff>
however, following code not functional
var b = new buffer(1); b.writeintbe(0xff, 0, 1); console.log(b);
the error code :
buffer.js:794 throw new typeerror('value out of bounds'); ^ typeerror: value out of bounds @ checkint (buffer.js:794:11) @ buffer.writeintbe (buffer.js:919:5) @ object.<anonymous> (/home/liuzeya/http/examples/buffer.js:2:3) @ module._compile (module.js:434:26) @ object.module._extensions..js (module.js:452:10) @ module.load (module.js:355:32) @ function.module._load (module.js:310:12) @ function.module.runmain (module.js:475:10) @ startup (node.js:117:18) @ node.js:951:3
help me understanding difference, please.
this can solved examining source at:
https://github.com/nodejs/node/blob/0a329d2d1f6bce2410d65ecd47175b5a4e1c1c91/lib/buffer.js
we can see call checkint:
checkint(this, value, offset, bytelength, math.pow(2, 8 * bytelength - 1) - 1, -math.pow(2, 8 * bytelength - 1));
working out math.pow's, see min -128 , max 127. if evaluate 0xff
alone in console, see positive 255, out of range.
so hexadecimal constants positive. i'm sure can find in javascript standard. whereas, you're using signed version of call. can have explicit negative hexadecimal values in javascript, looks freaking odd in opinion, -0x1
in case.
Comments
Post a Comment