Issue
This Content is from Stack Overflow. Question asked by pefr
I am searching to convert an array of byte ([]byte) to array of float64 ([]float64) but I didn’t have any clues to do it.
I read data from rtlsdr package (https://pkg.go.dev/github.com/jpoirier/gortlsdr) and would pass it to Pwelch algorithm (https://pkg.go.dev/github.com/mjibson/go-dsp/spectral)
Is it possible to convert large array of byte to float64 ?
(+ another question: did you think that possible to convert Pwelch function to work with uint8 ?)
Thank you so much,
pefr
Solution
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 🙂
This Question was asked in StackOverflow by pefr and Answered by some-user It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.