Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decoding bug of short int #25

Merged
merged 1 commit into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/util/ByteBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ ByteBuffer.prototype.getShort = function (index) {
}
var lower = this.buffer[index];
var upper = this.buffer[index + 1];
return (upper << 8) + lower;
var value = (upper << 8) + lower;
if (value & 0x8000) {
value = -((value - 1) ^ 0xFFFF);
}
return value
};

// Write integer to buffer by little endian
Expand Down
8 changes: 8 additions & 0 deletions test/util/ByteBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe("ByteBuffer static methods", function () {
byteBuffer = new ByteBuffer(50);
});

it("putShort() and getShort()", function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

var v = -413;
byteBuffer.putShort(v);
expect(byteBuffer.position).equals(2);
var got = byteBuffer.getShort(0);
expect(got).equals(v);
});

it("putString() and getString() 2 bytes UTF-8", function () {
var str = "âbcde";
byteBuffer.putString(str);
Expand Down