我正在嘗試弄清楚如何像這樣轉(zhuǎn)換NodeJS代碼:const buffer = new Buffer(24);offset = buffer.writeUInt32BE(this.a, offset);offset = buffer.writeUInt32BE(this.b, offset);offset = buffer.writeUInt8(this.c, offset);offset = buffer.writeUInt16BE(d, e); 1 : 0, offset);buffer.writeInt8(this.f, offset);去。我想我可以使用buffer := make([]byte, 24)buffer[0] = abuffer[2] = b但這不起作用有沒有推薦的方法來用Go做這樣的事情?
1 回答

慕村9548890
TA貢獻(xiàn)1884條經(jīng)驗 獲得超4個贊
您應(yīng)該使用二進(jìn)制文件。字節(jié)順序。
因此,在你的情況下,使用大端序,類似于:
package main
import (
"encoding/binary"
)
func main() {
buffer := make([]byte, 24)
// Uint32
binary.BigEndian.PutUint32(buffer, 1)
binary.BigEndian.PutUint32(buffer[4:], 2)
// Uint8
buffer[8] = 3
// Uint16
binary.BigEndian.PutUint16(buffer[9:], 4)
// Uint8
buffer[13] = 5
}
- 1 回答
- 0 關(guān)注
- 94 瀏覽
添加回答
舉報
0/150
提交
取消