美文网首页
Golang Int64与[]byte互转

Golang Int64与[]byte互转

作者: CommandM | 来源:发表于2018-02-15 16:06 被阅读2669次

原文:https://studygolang.com/topics/22

package main

import (
    "fmt"
    "encoding/binary"
)

func main() {
    var i int64 = 2323
    buf := Int64ToBytes(i)
    fmt.Println(buf)
    fmt.Println(BytesToInt64(buf))
}

func Int64ToBytes(i int64) []byte {
    var buf = make([]byte, 8)
    binary.BigEndian.PutUint64(buf, uint64(i))
    return buf
}

func BytesToInt64(buf []byte) int64 {
    return int64(binary.BigEndian.Uint64(buf))
}

相关文章

网友评论

      本文标题:Golang Int64与[]byte互转

      本文链接:https://www.haomeiwen.com/subject/mqkutftx.html