我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

如何在Golang中将16位整数写入多个字节?

Let's say I have the 16 bit integer 259 (0000000100000011 in binary) and I want to write it to a byte stream in Go. A byte is only 8 bits, so how can I split the integer across multiple bytes?

Use the binary.Write method of the encoding/binary package.

buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, uint16(259))
if err != nil {
    fmt.Println("binary.Write failed:", err)
}

// This should be two bytes with your encoded integer.
fmt.Println(buf.Bytes())

近期文章

  • JMX for Go [lang]
  • 我可以做getest pass编译器标志吗?
  • “ ConnectEx tcp:信号灯超时时间已到。”的含义是什么。
  • 为什么filepath.Walk()恐慌?
  • 解析XML命名空间节点
  • Golang从文件中读取数据并指示缺少值
  • SWIG:从Python调用Go
  • Docker Compose找不到本地软件包
  • 进行错误分组的最佳方法是什么?
  • 来自go的COM调用通过GC收集了其数据,将已用内存清零
  • 在SSH客户端中为Golang创建文件
  • GoREST端点路径
  • 如何在Go中保持工厂功能?
  • RegEx全部匹配,直到两个连续的特殊字符(]])
  • 我该如何设置go-gb以在vim内部构建
  • 在go中将数组编组为单个xml元素
  • 如何漂亮地打印字节数组和char数组?
  • 鉴于以下可能性,最有效的ETag生成是什么?
  • GoSublime-可以在代码完成弹出窗口旁边显示文档吗?
  • 本地部署工作时,远程appengine-go静态文件上出现404错误

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华