你如何在Go语言中将整数编码为LEB128格式?我试图将int32编码为Minecraft VarInt,迄今为止,我已经尝试将维基百科上的示例导入Go中。然而,在测试时,我得到了错误的结果。维基百科说-1应该等于[255 255 255 255 15],但我得到的是[255 255 255 255 255]。我做错了什么?
func WriteVarInt2(v int32) []byte{
var out []byte
c := 0
for{
currentByte := byte(v & 0b01111111)
v >>= 7
if v != 0 {
currentByte |= 0b10000000
}
out = append(out, currentByte)
c++
if c >= 5 || v == 0{
return out
}
}
}