This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Knut Ahlers <knut@ahlers.me>
- Loading branch information
Showing
3 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package sii | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func float2sii(f float32) ([]byte, error) { | ||
var ( | ||
buf = new(bytes.Buffer) | ||
err error | ||
) | ||
|
||
err = binary.Write(buf, binary.BigEndian, f) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Unable to encode float") | ||
} | ||
|
||
dst := make([]byte, hex.EncodedLen(buf.Len())) | ||
hex.Encode(dst, buf.Bytes()) | ||
|
||
return append([]byte("&"), dst...), nil | ||
} | ||
|
||
func sii2float(f []byte) (float32, error) { | ||
if f[0] != '&' { | ||
out, err := strconv.ParseFloat(string(f), 32) | ||
return float32(out), err | ||
} | ||
|
||
// Strip leading '&' | ||
f = f[1:] | ||
|
||
var ( | ||
err error | ||
out float32 | ||
) | ||
|
||
dst := make([]byte, hex.DecodedLen(len(f))) | ||
_, err = hex.Decode(dst, f) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "Unable to read hex format") | ||
} | ||
|
||
err = binary.Read(bytes.NewReader(dst), binary.BigEndian, &out) | ||
return out, errors.Wrap(err, "Unable to decode hex notation") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package sii | ||
|
||
import "testing" | ||
|
||
func TestSii2FloatConversion(t *testing.T) { | ||
var ( | ||
err error | ||
f float32 | ||
) | ||
|
||
for b, exp := range map[string]float32{ | ||
"0.00250711967": 0.00250711967, | ||
"1.0": 1.0, | ||
"&3b244e7d": 0.00250711967, | ||
"&3f46da61": 0.7767697, | ||
"&47135818": 37720.0938, | ||
} { | ||
f, err = sii2float([]byte(b)) | ||
if err != nil { | ||
t.Errorf("Conversion of %q failed: %s", b, err) | ||
continue | ||
} | ||
|
||
if f != exp { | ||
t.Errorf("Conversion of %q has unxpected result: %f != %f", b, f, exp) | ||
} | ||
} | ||
} | ||
|
||
func TestFloat2SiiConversion(t *testing.T) { | ||
var ( | ||
err error | ||
f []byte | ||
) | ||
|
||
for b, exp := range map[float32]string{ | ||
0.00250711967: "&3b244e7d", | ||
0.7767697: "&3f46da61", | ||
37720.0938: "&47135818", | ||
} { | ||
f, err = float2sii(b) | ||
if err != nil { | ||
t.Errorf("Conversion of %f failed: %s", b, err) | ||
continue | ||
} | ||
|
||
if string(f) != exp { | ||
t.Errorf("Conversion of %f has unxpected result: %s != %s", b, f, exp) | ||
} | ||
} | ||
} |