我目前正在尝试使用字节。我不想将Short
和Long
值写入String
。
来自HEX
的{{1}}(不是JavaScript!)中的原始输出如下:
00 15 FF FF FF FF FF FF D0 3F
但是当我在Java
JavaScript
工作中创建的内容不正确时:
00 15 EE 47 AF 15 EE 47 AF 15
这是我工作的JSFiddle
writeLong

class BufferWriter {
constructor(buffer) {
this._buffer = buffer;
}
/*
00 15 FF FF FF FF FF FF D0 3F
*/
writeByte(value) {
this._buffer.append(value & 0xFF);
}
writeInt(value) {
[ 24, 16, 8, 0 ].forEach(function(shift) {
this.writeByte(value >>> shift);
}.bind(this));
}
writeShort(value) {
[ 8, 0 ].forEach(function(shift) {
this.writeByte(value >>> shift);
}.bind(this));
}
writeLong(value) {
[ 56, 48, 40, 32, 24, 16, 8, 0 ].forEach(function(shift) {
this.writeByte(value >>> shift);
}.bind(this));
}
}
class Buffer {
constructor() {
this._buffer = [];
}
append(data) {
console.log(data, String.fromCharCode(data));
this._buffer.push(data);
}
toString() {
return String.fromCharCode.apply(null, this._buffer);
}
toHEX() {
return Array.prototype.map.call(this._buffer, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('').toUpperCase();
}
getSize() {
return this._buffer.length;
}
}