英文:
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?
答案1
得分: 22
使用math.Float64bits将float64
转换为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/binary将uint64
转换为字节,而不是直接编写位移和类型转换操作。以下是使用该包以大端序编码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[:]
}
英文:
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[:]
}
答案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()
}
答案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.Buffer
和 binary.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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论