gpt4 book ai didi

node.js - 等价于Golang中的readUIntLE?

转载 作者:行者123 更新时间:2023-12-01 21:58:24 26 4
gpt4 key购买 nike

我需要在缓冲区的Y位置读取X(例如3个)字节。

在Node.js中,我通过使用Buffer类和readUIntLE函数来做到这一点。

例如:readUIntLE(position, 3)

Golang中的该过程相当于什么?

谢谢!

最佳答案

例如,

package main

import "fmt"

func readUIntLE(buf []byte, offset, byteLength int) uint64 {
var n uint64
buf = buf[offset : offset+byteLength]
if len(buf) > 8 {
buf = buf[:8]
}
for i, b := range buf {
n += uint64(b) << uint(8*i)
}
return n
}

func main() {
buf := []byte{2, 4, 8, 16, 32, 64, 128, 255}
fmt.Println(buf)
fmt.Println(readUIntLE(buf, 0, 4))
fmt.Println(readUIntLE(buf, 0, len(buf)))
fmt.Println(readUIntLE(buf, len(buf)-1, 1))
}

输出:
[2 4 8 16 32 64 128 255]
268960770
18410785783142679554
255

关于node.js - 等价于Golang中的readUIntLE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30152150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com