在GoLang中将字节片“ [] uint8”转换为float64
我正在尝试将[]uint8字节切片转换为float64GoLang中的。我找不到在线解决此问题的方法。我见过先转换为字符串然后转换为a的建议,float64但这似乎不起作用,它失去了它的值,最后以零结束。
例:
metric.Value, _ = strconv.ParseFloat(string(column.Value), 64)而且它不起作用…
回答:
例如,
package mainimport (
    "encoding/binary"
    "fmt"
    "math"
)
func Float64frombytes(bytes []byte) float64 {
    bits := binary.LittleEndian.Uint64(bytes)
    float := math.Float64frombits(bits)
    return float
}
func Float64bytes(float float64) []byte {
    bits := math.Float64bits(float)
    bytes := make([]byte, 8)
    binary.LittleEndian.PutUint64(bytes, bits)
    return bytes
}
func main() {
    bytes := Float64bytes(math.Pi)
    fmt.Println(bytes)
    float := Float64frombytes(bytes)
    fmt.Println(float)
}
输出:
[24 45 68 84 251 33 9 64]3.141592653589793
以上是 在GoLang中将字节片“ [] uint8”转换为float64 的全部内容, 来源链接: utcz.com/qa/425513.html




