Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 相当于Golang的readUIntLE?_Node.js_Go - Fatal编程技术网

Node.js 相当于Golang的readUIntLE?

Node.js 相当于Golang的readUIntLE?,node.js,go,Node.js,Go,我需要读取缓冲区中Y位置的X(例如,3)字节 在Node.js中,我使用Buffer类和readUIntLE函数来实现这一点 例如:readUIntLE(位置3) 在戈朗,这一过程的等价物是什么 谢谢 例如 package main import "fmt" func readUIntLE(buf []byte, offset, byteLength int) uint64 { var n uint64 buf = buf[offset : offset+byteLength

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

在Node.js中,我使用Buffer类和readUIntLE函数来实现这一点

例如:
readUIntLE(位置3)

在戈朗,这一过程的等价物是什么

谢谢

例如

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))
}
比如说,

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))
}
比如说,

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))
}
比如说,

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))
}

如果你有一个字节数组,它只是一个
buffer[3]
@inf,你能提供一个例子吗?buffer:=[]字节(“asdf”);log.Println(缓冲区[2])
readUIntLE(位置,3)
不“读取一个字节”,它读取缓冲区中
position
处的3个字节,并将这些字节转换为无符号24位整数(小尾数)。@msdex谢谢,我更新了这个问题。如果你有一个字节数组,它就是
buffer[3]
@inf你能提供一个例子吗?buffer:=[]字节(“asdf”); log.Println(缓冲区[2])
readUIntLE(位置,3)
不“读取一个字节”,它读取缓冲区中
position
处的3个字节,并将这些字节转换为无符号24位整数(小尾数)。@msdex谢谢,我更新了这个问题。如果你有一个字节数组,它就是
buffer[3]
@inf你能提供一个例子吗?buffer:=[]字节(“asdf”); log.Println(缓冲区[2])
readUIntLE(位置,3)
不“读取一个字节”,它读取缓冲区中
position
处的3个字节,并将这些字节转换为无符号24位整数(小尾数)。@msdex谢谢,我更新了这个问题。如果你有一个字节数组,它就是
buffer[3]
@inf你能提供一个例子吗?buffer:=[]字节(“asdf”); log.Println(缓冲区[2])
readUIntLE(位置,3)
不“读取一个字节”,它读取缓冲区中
position
处的3个字节,并将这些字节转换为无符号24位整数(小尾端)。@mscdex谢谢,我更新了问题。