GolangWebDev
GolangWebDev
3092 0 0

The way Convert an integer to a byte array with golang

Use the encoding/binary library

package main

import (
    "encoding/binary"
    "fmt"
)

func main() {
    bs := make([]byte, 4)
    binary.LittleEndian.PutUint32(bs, 31415926)
    fmt.Println(bs)
}

use int -> string -> byte

bs := []byte(strconv.Itoa(31415926))
binary.Write(a, binary.LittleEndian, myInt)
binary.LittleEndian.PutUint32(bs, value)
binary.BigEndian.PutUint32(b, i)
0

See Also


Discussion

Login Topics