Arrays “转换字节片”;[]uint8“;去戈朗
我试图在GoLang中将Arrays “转换字节片”;[]uint8“;去戈朗,arrays,go,uint8t,Arrays,Go,Uint8t,我试图在GoLang中将[]uint8字节片转换为float64。我无法在网上找到此问题的解决方案。我看到过一些建议,先转换为字符串,然后再转换为float64,但这似乎不起作用,它会丢失它的值,结果是零 例如: metric.Value, _ = strconv.ParseFloat(string(column.Value), 64) 而且它不起作用…我认为Go文档中的这个示例就是您想要的: 打印3.141592653589793当注释读取时,这完全取决于您的[]uint8切片中的数据类型
[]uint8
字节片转换为float64
。我无法在网上找到此问题的解决方案。我看到过一些建议,先转换为字符串,然后再转换为float64
,但这似乎不起作用,它会丢失它的值,结果是零
例如:
metric.Value, _ = strconv.ParseFloat(string(column.Value), 64)
而且它不起作用…我认为Go文档中的这个示例就是您想要的:
打印3.141592653589793当注释读取时,这完全取决于您的
[]uint8
切片中的数据类型
如果它是以小端顺序表示IEEE 754浮点值的字节,则使用Kluig或peterSo(不使用反射的更好性能)的答案
如果它是拉丁语1/UTF-8编码的文本表示,那么您应该能够做到刚才所做的:
package main
import (
"fmt"
"strconv"
)
func main() {
var f float64
text := []uint8("1.23") // A decimal value represented as Latin-1 text
f, err := strconv.ParseFloat(string(text), 64)
if err != nil {
panic(err)
}
fmt.Println(f)
}
结果:
1.23
操场:例如
package main
import (
"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
我希望这个黑客能有所帮助。它的目的是将二进制数的长流转换为浮点数 例如: 01101111000010100000111000000100100001110000001001001000010000011100000010000111000000111000000->-3.1415
func binFloat(bin string) float64 {
var s1 []byte
var result float64
if len(bin) % 8 == 0 {
for i := 0; i < len(bin) / 8; i++ {
//Chop the strings into a segment with a length of 8.
//Convert the string to Integer and to byte
num, _ := strconv.ParseInt(bin[8*i: 8*(i + 1)], 2, 64)
//Store the byte into a slice s1
s1 = append(s1, byte(num))
}
}
//convert the byte slice to a float64.
//The algorithm below are copied from golang binary examples.
buf := bytes.NewReader(s1)
//You can also change binary.LittleEndian to binary.BigEndian
//For the details of Endianness, please google Endianness
err := binary.Read(buf, binary.LittleEndian, &result)
if err != nil {
panic(err)
fmt.Println("Length of the binary is not in the length of 8")
}
return result
}
func binFloat(bin字符串)float64{
变量s1[]字节
var结果浮动64
如果len(bin)%8==0{
对于i:=0;i
字节数组包含什么?发布一个数据示例如果您意外地得到一个零值并忽略返回的错误,也许值得检查该错误?FWIW我也有同样的问题(转换后得到0),我的问题是我的[]字节中有一个空格/行终止符字符(将%s转储到stdout时不可见),因此我使用strconv.ParseFloat(strings.TrimSpace(string(b)),64修复了它)奇怪的是,你默认使用的是little endian。网络字节顺序在互联网上越来越多,而且…ma问sense。这就是它,只是我们使用的是BigEndian。谢谢先生!little endian现在几乎是通用的。即使是现代网络协议也使用它(这是有道理的——实际上所有处理器都是小端的,所以您只需在两端毫无意义地交换字节)。请在发布代码时更加清楚,发布代码和示例有时是不够的,请编辑您的答案并进行更多解释。我将相应地进行修改。这是我关于StackOverflow的第一篇文章。很抱歉没有遵守StackOverflow中的规定。
func binFloat(bin string) float64 {
var s1 []byte
var result float64
if len(bin) % 8 == 0 {
for i := 0; i < len(bin) / 8; i++ {
//Chop the strings into a segment with a length of 8.
//Convert the string to Integer and to byte
num, _ := strconv.ParseInt(bin[8*i: 8*(i + 1)], 2, 64)
//Store the byte into a slice s1
s1 = append(s1, byte(num))
}
}
//convert the byte slice to a float64.
//The algorithm below are copied from golang binary examples.
buf := bytes.NewReader(s1)
//You can also change binary.LittleEndian to binary.BigEndian
//For the details of Endianness, please google Endianness
err := binary.Read(buf, binary.LittleEndian, &result)
if err != nil {
panic(err)
fmt.Println("Length of the binary is not in the length of 8")
}
return result
}