作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在缓冲区的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/
我是一名优秀的程序员,十分优秀!