On Fri, May 18, 2018 at 12:26 PM,  <john.macrae....@gmail.com> 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

You've got a 2 byte slice, but you need 8 bytes. You're getting an
'index out of range' because you need a slice of 8 bytes.


> Attempt 3 = Bytes to bytes.Reader to binary.Read(slice of bytes,
> binary.LittleEndian, floatVariableToFill) to error check

You've got 2 bytes in your bytes.Reader and you're trying to read 8
bytes from it, this is why you get an error.
A float64 is 8 bytes so you need at least 8 bytes in your bytes.Reader.

> Attempt 2 = bytes to string, String to float (strconv)

You are parsing the string "2\n" which isn't a string representation
of a float. So that's not going to work.
The fact that these bytes are ascii characters means that your other
attempts don't make a lot of sense. The ascii value for the character
'2' is 50 so even if your other attempts worked you'd get a float with
50 instead of the 2 you're expecting.

If you do f, err :=
strconv.ParseFloat(strings.TrimSpace(string(text)), 64) you'll trim
off the invalid '\n' and you'll just have a '2' that will parse
correctly.

-- 
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