There is a function for this in the math lib Float64Frombits, a similar 
question: 
https://stackoverflow.com/questions/22491876/convert-byte-array-uint8-to-float64-in-golang

On Friday, 18 May 2018 10:31:11 UTC+8, John Fox wrote:
>
> Hello all.  I am creating a custom exporter for FreeNAS 
> https://github.com/Maelos/freenas_exporter and am stuck on the conversion 
> of the string of bytes provided by the commands output to a float.  Here is 
> my code, what I have tried, and my results:
>
> What I have tried and results (commented so you can try and see each 
> https://play.golang.org/p/sevfk7Nt2w4
>
> Attempt 1 = binary.LittleEndian.Uint64([]bytes) to math.Float64frombits
> Attempt 2 = bytes to string, String to float (strconv)
> Attempt 3 = Bytes to bytes.Reader to binary.Read(slice of bytes, 
> binary.LittleEndian, floatVariableToFill) to error check
>
> Please let me know what more I can provide.  I will keep hacking away at 
> it tomorrow, but hoping I can get this done soon.  Full go play write up 
> below:
>
> package main
>
>
> import (
>  //You may need to edit these in or out depending upon the attempt
>  "bytes"
>  "encoding/binary"
>  "fmt"
>  //"strconv"
>  //"math"
> )
>
>
> func main() {
>
>
>  // I have been able to test this part and I do get a return of "[50 10]" 
> which is correct (converts string "2" for 2 CPUs).  The exact statement may 
> be a bit different.
>  // I am running the script on the latest FreeNAS whic his built from 
> 11.1STABLE FreeBSD.  I have Go installed and am building the files on the 
> shell
>  //numCPUCmd := exec.Command("bash", "-c","/usr/local/bin/ipmitool -I 
> lanplus -H ipmiAddress -U ipmiUser -f /root/ipmi_password sdr elist all | 
> grep -c -i \"cpu.*temp\"")
>  //numCPUBytes, _ := numCPUCmd.Output() //returns a slice of bytes and an 
> error
>
>
>
>
>  // ATTEMPT 1
>  var flty float64
>  sob := []byte{50, 10}
>  fmt.Printf("\n%T %v\n\n", sob, sob)
>
>
>  buf := bytes.NewReader(sob)
>  err := binary.Read(buf, binary.LittleEndian, &flty)
>  if err != nil {
>  fmt.Println("binary.Read failed:", err)
>  }
>  fmt.Println(flty)
>  
>  /*Result
>  []uint8 [50 10]
>
>
>  binary.Read failed: unexpected EOF
>  0
>  */
>  
>  ////////////////////////////////////////////////////////
>  //ATTEMPT 2
>  /*
>  var f float64
>     text := []byte{50, 10} // A decimal value represented as Latin-1 text
>
>
>     f, err := strconv.ParseFloat(string(text), 64)
>     if err != nil {
>         panic(err)
>     }
>     fmt.Println(f)
>  */
>  /*Result
>  panic: strconv.ParseFloat: parsing "2\n": invalid syntax
>
>
>  goroutine 1 [running]:
>  main.main()
>  /tmp/sandbox657430918/main.go:44 +0x160
>  */
>  
>  ////////////////////////////////////////////////
>  //ATTEMPT3
>  /*
>  sob := []byte{50, 10}
>  bits := binary.LittleEndian.Uint64(sob)
>  fmt.Printf("\n\n%T %v\n\n", bits, bits)
>
>
>  flty := math.Float64frombits(bits)
>  fmt.Printf("\n\n%T %v\n\n", flty, flty)
>
>
>  inty := int(flty)
>  fmt.Printf("\n\n%T %v\n\n", inty, inty)
>  */
>  /* Result
>  panic: runtime error: index out of range
>
>
>  goroutine 1 [running]:
>  encoding/binary.binary.littleEndian.Uint64(...)
>  /usr/local/go/src/encoding/binary/binary.go:76
>  main.main()
>  /tmp/sandbox742704811/main.go:62 +0x20
>  */
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to