stringgofloating-pointbinaryuint16

uint16 to xx.x float -- fastest algorithm?


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?


Solution

  • 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