javascript - 如何在 node.js 中实现 readUInt16BE 函数

标签 javascript node.js go byte

<分区>

node.js中的readUint16BE函数,函数声明为:

buf.readUInt16BE(offset, [noAssert])

文档: http://nodejs.org/api/buffer.html#buffer_buf_readuint16be_offset_noassert

Reads an unsigned 16 bit integer from the buffer at the specified offset with specified endian format.

在golang中如何实现?非常感谢

或者 golang 有一些像 readUint16BE 这样的函数 ??

最佳答案

您可以使用编码/二进制包。 例如:(http://play.golang.org/p/5s_-hclYJ0)

package main

import (
    "encoding/binary"
    "fmt"
)

func main() {
    buf := make([]byte, 1024)

    // Put uint16 320 (big endian) into buf at offster 127
    binary.BigEndian.PutUint16(buf[127:], 320)

    // Put uint16 420 (little endian) into buf at offster 127
    binary.LittleEndian.PutUint16(buf[255:], 420)

    // Retrieve the uint16 big endian from buf
    result := binary.BigEndian.Uint16(buf[127:])
    fmt.Printf("%d\n", result)

    // Retrieve the uint16 little endian from buf
    result = binary.LittleEndian.Uint16(buf[255:])
    fmt.Printf("%d\n", result)

    // See the state of buf (display only 2 bytes from the given often as it is uint16)
    fmt.Printf("%v, %v\n", buf[127:129], buf[255:257])
}

关于javascript - 如何在 node.js 中实现 readUInt16BE 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18990655/

相关文章:

javascript - 响应式下拉菜单正在重新加载我的页面

javascript - 下雪......但只有在鼠标移动时?

javascript - Angular渲染模板并将其存储到本地

google-app-engine - 是否可以使用从不同版本构建的 Go 包?

go - 接口(interface)转换 : interface {} is uint64, 不是 float32

interface - 如何实现可以接受可以在 golang 中进行相等性测试的任何类型的链表?

javascript - 未捕获的类型错误 : Object #<HTMLDivElement> has no method 'attr'

node.js - fluent-ffmpeg display_aspect_ratio 没有改变

javascript - NodeJS 与 connect-busboy 发生错误, "TypeError: Cannot call method ' 未定义”

node.js - 清晰的手动模拟 Jest