On Fri, Oct 3, 2025 at 7:13 PM Rafael Mendoza
<
rafael.m...@gmail.com> wrote:
> Hi Jan, thanks for the reply but perhaps I didn't explain correctly. I didn't mean to say the varint itself had any endianess but rather the decoded uint itself when the uint is larger than 1 byte. For >=2 bytes uint, Uvarint function is taking the most significant byte and placing it as the least significant byte before the concatenation step. So the resulting uint (in this case uint16) is different than the expected result.
It's hard to say much without seeing your code. Here's some code and
what it produces on LE/BE targets:
package main
import (
"encoding/binary"
"fmt"
"runtime"
"unsafe"
)
func main() {
fmt.Printf("goos=%s goarch=%s\n", runtime.GOOS, runtime.GOARCH)
buf := [...]byte{0x11, 0x22, 0x33, 0x44}
fmt.Printf("buf=|% x|\n", buf)
fmt.Printf("unsafe uint32 at buf=%#0x\n",
*(*uint32)(unsafe.Pointer(&buf[0])))
s := buf[:]
num := uint64(0x1234)
s = s[:binary.PutUvarint(s, num)]
fmt.Printf("s=|% x|\n", s)
u, n := binary.Uvarint(s)
fmt.Printf("u=%#0x n=%v\n", u, n)
}
----
jnml@3900x:~/tmp/uvarint$ go run main.go
goos=linux goarch=amd64
buf=|11 22 33 44|
unsafe uint32 at buf=0x44332211
s=|b4 24|
u=0x1234 n=2
jnml@3900x:~/tmp/uvarint$
----
jnml@linux-s390x:~/tmp/uvarint$ go run main.go
goos=linux goarch=s390x
buf=|11 22 33 44|
unsafe uint32 at buf=0x11223344
s=|b4 24|
u=0x1234 n=2
jnml@linux-s390x:~/tmp/uvarint$
----
The encoding buffer `s` always contains the same byte sequence and
that sequence always decodes to the same number.
HTH
-j