duanbanzhi4419 2015-05-10 16:18
浏览 221
已采纳

Golang的_byteswap_ulong

What is the equivalent of _byteswap_ulong in Golang? Does it exist as a package?

I tried to use the binary package and play with the reader, but couldn't get it working. I need to swap bytes in a uint64 variable.

Input is 2832779. Output should be 8b392b.

  • 写回答

2条回答 默认 最新

  • dpbl91234 2015-05-10 17:11
    关注

    As @David said, use the binary.ByteOrder type

    package main
    
    import (
        "encoding/binary"
        "fmt"
    )
    
    func main() {
        value := make([]byte, 4)
    
        // need to know the byte ordering ahead of time
        binary.LittleEndian.PutUint32(value, 0x12345678)
    
        fmt.Printf("%#v
    ", value)
    
        fmt.Printf("Big Endian Representation, 0x%X
    ", binary.BigEndian.Uint32(value))
        fmt.Printf("Little Endian Representation, 0x%X
    ", binary.LittleEndian.Uint32(value))
    }
    

    Playground link

    This will output:

    []byte{0x78, 0x56, 0x34, 0x12}
    Big Endian Representation, 0x78563412
    Little Endian Representation, 0x12345678
    

    These are the big-endian and little-endian representations on a little-endian server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题