0

I receive data from a temperature sensor that is formatted as two bytes which are a binary encoding of the fixed-width, floating point data. The encoding scheme can be demonstrated in the following table:

| temperature | hexadecimal | int16 |
| 0.1 *F      | 0x00 0x01   | 01    |
| 10.0 *F     | 0x00 0x64   | 100   |

To reconstruct the floating point value, I have written the following short program:

package main

import (
    "encoding/binary"
    "fmt"
    "strconv"
)

func main() {
    b1 := byte(0x02)
    b2 := byte(0xBC)
    b := []byte{b1, b2}
    intgr := binary.BigEndian.Uint16(b)
    str := fmt.Sprint(intgr)
    l := len(str)
    front := str[:l-1]
    decimal := str[l-1:]
    str = fmt.Sprintf("%v.%v", front, decimal)
    float, _ := strconv.ParseFloat(str, 64)
    fmt.Println(float)
}

However it is a bit too slow for my needs, which I think is because I have used Sprint/Sprintf. Is there a faster (and perhaps cleaner) algorithm for doing this?

Brandon Dube
  • 428
  • 1
  • 10
  • 26

1 Answers1

1

Convert the bytes to a number and divide by 10:

b := []byte{0x02, 0xBC}
f := float64(binary.BigEndian.Uint16(b)) / 10.0

The reverse conversion is:

p := make([]byte, 2)
binary.BigEndian.PutUint16(p, uint16(math.Round(f*10.0)))

To handle negative numbers, covert the uint16 to an int16 before the conversion to float:

f := float64(int16(binary.BigEndian.Uint16(p))) / 10.0
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
  • Is there a similarly quick way to go the opposite direction? I need to write to these as well, albeit at a far slower rate. Algorithmic symmetry would be ideal. – Brandon Dube Sep 30 '19 at 22:44
  • @Brandon said: `this is much faster...` I just ran a test and Uint16() is inlined as I suspected. `.\main.go:13:34: inlining call to binary.bigEndian.Uint16` Personally I would use the std library function, as it as just as fast, self-documenting, and avoids possible mistakes using bit manipulation (shifts, OR, etc). – Andrew W. Phillips Oct 01 '19 at 00:15
  • I agree using the binary pkg to do the manipulation is better — it’s the lack of round tripping through text that make Cerise’ answer much faster than what I came up with. – Brandon Dube Oct 01 '19 at 04:32