将float64转换为字节数组。

huangapple go评论139阅读模式
英文:

Convert float64 to byte array

问题

你好!以下是你要翻译的内容:

如何将一个float64变量转换为大端编码的字节数组?

var f float64 = 12.666512
var result []byte = float64ToByte(f);
fmt.Printf("result:%f",result)

为了清晰起见,我应该如何在下面的playground中实现float64ToByte函数?

https://play.golang.org/p/LevxCDd7mK

英文:

How can I convert a float64 variable to big endian encoded byte array?

var f float64 = 12.666512
var result []byte = float64ToByte(f);
fmt.Printf("result:%f",result)

For the sake of clarity how should I implement float64ToByte function in the following playground?

https://play.golang.org/p/LevxCDd7mK

答案1

得分: 22

使用math.Float64bitsfloat64转换为uint64。使用位移和类型转换操作将uint64转换为所需的字节序列。例如,以下是如何以大端序编码浮点数的示例:

var buf [8]byte
n := math.Float64bits(f)
buf[0] = byte(n >> 56)
buf[1] = byte(n >> 48)
buf[2] = byte(n >> 40)
buf[3] = byte(n >> 32)
buf[4] = byte(n >> 24)
buf[5] = byte(n >> 16)
buf[6] = byte(n >> 8)
buf[7] = byte(n)

你可以使用encoding/binaryuint64转换为字节,而不是直接编写位移和类型转换操作。以下是使用该包以大端序编码float64的示例:

var buf [8]byte
binary.BigEndian.PutUint64(buf[:], math.Float64bits(f))

小端序的代码如下:

var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], math.Float64bits(f))

以下是问题中float64ToByte函数的大端序实现:

func float64ToByte(f float64) []byte {
   var buf [8]byte
   binary.BigEndian.PutUint64(buf[:], math.Float64bits(f))
   return buf[:]
}

playground示例

英文:

Use math.Float64bits to get the float64 as a uint64. Use shifting and conversions on the uint64 to convert to a desired sequence of bytes. For example, here's how to encode a float in big endian order:

var buf [8]byte
n := math.Float64bits(f)
buf[0] = byte(n >> 56)
buf[1] = byte(n >> 48)
buf[2] = byte(n >> 40)
buf[3] = byte(n >> 32)
buf[4] = byte(n >> 24)
buf[5] = byte(n >> 16)
buf[6] = byte(n >> 8)
buf[7] = byte(n)

You can use the encoding/binary to convert the uint64 to bytes instead of writing out the shifts and conversions directly. Here's how to encode the float64 in big endian order using that package:

var buf [8]byte
binary.BigEndian.PutUint64(buf[:], math.Float64bits(f))

The little endian code is:

var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], math.Float64bits(f))

Here's the big endian implementation of the float64ToByte function in the question:

func float64ToByte(f float64) []byte {
   var buf [8]byte
   binary.BigEndian.PutUint64(buf[:], math.Float64bits(f))
   return buf[:]
}

playground example

答案2

得分: 11

你可以使用encoding/binary包中的binary.Write()函数:

func float64ToByte(f float64) []byte {
    var buf bytes.Buffer
    err := binary.Write(&buf, binary.BigEndian, f)
    if err != nil {
        fmt.Println("binary.Write failed:", err)
    }
    return buf.Bytes()
}

链接:https://play.golang.org/p/XcvM5eaGtU

英文:

You can use binary.Write() from package "encoding/binary" :

func float64ToByte(f float64) []byte {
	var buf bytes.Buffer
	err := binary.Write(&buf, binary.BigEndian, f)
	if err != nil {
		fmt.Println("binary.Write failed:", err)
	}
	return buf.Bytes()
}

https://play.golang.org/p/XcvM5eaGtU

答案3

得分: 0

在我的系统上,它必须是小端序的:

func float64ToByte(f float64) []byte {
    var buf bytes.Buffer
    err := binary.Write(&buf, binary.LittleEndian, f)
    if err != nil {
        fmt.Println("binary.Write failed:", err)
    }
    return buf.Bytes()
}

在这段代码中,float64ToByte 函数将一个 float64 类型的数转换为字节切片。它使用了 bytes.Bufferbinary.Write 函数来实现。binary.Write 函数将 f 写入到 buf 中,并按照小端序进行编码。如果出现错误,会打印错误信息。最后,返回 buf 的字节表示。

英文:

https://play.golang.org/p/FO32EmWfjbL

On my system it had to be little endianed:

func float64ToByte(f float64) []byte {
	var buf bytes.Buffer
	err := binary.Write(&buf, binary.LittleEndian, f)
	if err != nil {
		fmt.Println("binary.Write failed:", err)
	}
	return buf.Bytes()
}

huangapple
  • 本文由 发表于 2017年4月29日 16:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/43693360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定