[go] 바이너리 데이터를 부동 소수점 숫자로 디코딩하는 방법
아래는 Go 언어를 사용하여 바이너리 데이터를 부동 소수점 숫자로 디코딩하는 예제 코드입니다.
package main
import (
"encoding/binary"
"fmt"
"math"
)
func main() {
byteData := []byte{0x40, 0x49, 0x0f, 0xdb} // example byte array representing a float64 number
// assuming the byteData represents a float64 in big-endian format
bits := binary.BigEndian.Uint64(byteData)
floatValue := math.Float64frombits(bits)
fmt.Println(floatValue)
}
이 예제에서는 encoding/binary
패키지의 Uint64
함수를 사용하여 바이너리 데이터를 부동 소수점 숫자로 디코딩하고 있습니다. 이때, math
패키지의 Float64frombits
함수를 사용하여 수를 부동 소수점 수로 변환합니다.
이 예제 코드를 실행하면 바이너리 데이터가 부동 소수점 숫자로 성공적으로 디코딩될 것입니다.
참고 문헌:
- https://pkg.go.dev/encoding/binary
- https://pkg.go.dev/math