写入变量整数时ByteBuffer.js出现意外结果

时间:2015-02-05 18:35:14

标签: javascript bytebuffer

下面,我试图将0x16写为变量整数。我期待看到0x2C,但我得到0x16。知道如何调整我对ByteBuffer的使用以获得预期的结果吗?请按照以下评论:

http://jsfiddle.net/jslim180/h1ojuc54/

ByteBuffer = window.dcodeIO.ByteBuffer

b = new ByteBuffer(DEFAULT_CAPACITY=4, ByteBuffer.LITTLE_ENDIAN)

console.log '22 decimal is 0x16 hex: ' + (22).toString(16)

# 22 can be represented in less than 7 bits so the least significant bit 
# should be 0 (indicating that no additional bytes are needed)
b.writeVarint32 22 
b.printDebug() # Not expected: prints 0x16 .. this did not bitshift at all

# If I bit-shift manually, I get the expected result: 0x2C
console.log (0x16<<1).toString(16) # prints 2c

https://github.com/dcodeIO/ByteBuffer.js/wiki/API

(顺便说一句:这是coffeescript,没有括号的javascript)

1 个答案:

答案 0 :(得分:0)

ByteBuffer正在写出无符号的varint。我需要使用writeVaruint32。 https://github.com/dcodeIO/ByteBuffer.js/issues/44