How can I convert an int64 into a byte array in go?
You can use this too:
var num int64 = -123456789
b := []byte(strconv.FormatInt(num, 10))
fmt.Printf("num is: %v, in string is: %s", b, string(b))
Output:
num is: [45 49 50 51 52 53 54 55 56 57], in string is: -123456789
Converting between int64
and uint64
doesn't change the sign bit, only the way it's interpreted.
You can use Uint64
and PutUint64
with the correct ByteOrder
http://play.golang.org/p/wN3ZlB40wH
i := int64(-123456789)
fmt.Println(i)
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(i))
fmt.Println(b)
i = int64(binary.LittleEndian.Uint64(b))
fmt.Println(i)
output:
-123456789
[235 50 164 248 255 255 255 255]
-123456789