How do you convert array of byte to array of float64

Viewed 32
1 Answers

byte/uint8 has 8 bits, float64 has 64. You can convert [8]byte to float64 like this:

package main

import (
    "encoding/binary"
    "fmt"
    "math"
)

main()
{
    in := 1.

    // Convert float64 to [8]byte
    var bytes [8]byte
    binary.LittleEndian.PutUint64(bytes[:], math.Float64bits(in))

    // Convert [8]byte to float64
    out := math.Float64frombits(binary.LittleEndian.Uint64(bytes[:]))

    fmt.Println(out)
}

You may want to chose binary.BigEndian instead.

Is that what you are looking for? Or do you want to convert an array of byte with length 8n to an array of float64 with length n? Feel free to comment :-)

Related