Last active
July 21, 2019 13:07
-
-
Save tosone/947653f66b9ab786252bf9b32c0edd35 to your computer and use it in GitHub Desktop.
[Byte to Float] #Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"encoding/binary" | |
"math" | |
) | |
func Float32ToByte(float float32) []byte { | |
bits := math.Float32bits(float) | |
bytes := make([]byte, 4) | |
binary.LittleEndian.PutUint32(bytes, bits) | |
return bytes | |
} | |
func ByteToFloat32(bytes []byte) float32 { | |
bits := binary.LittleEndian.Uint32(bytes) | |
return math.Float32frombits(bits) | |
} | |
func Float64ToByte(float float64) []byte { | |
bits := math.Float64bits(float) | |
bytes := make([]byte, 8) | |
binary.LittleEndian.PutUint64(bytes, bits) | |
return bytes | |
} | |
func ByteToFloat64(bytes []byte) float64 { | |
bits := binary.LittleEndian.Uint64(bytes) | |
return math.Float64frombits(bits) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment