forked from fiorix/go-diameter
/
float32.go
49 lines (40 loc) · 1.04 KB
/
float32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2013-2015 go-diameter authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package datatype
import (
"encoding/binary"
"fmt"
"math"
)
// Float32 data type.
type Float32 float32
// DecodeFloat32 decodes a Float32 data type from a byte array.
func DecodeFloat32(b []byte) (Type, error) {
if len(b) != 4 {
return Float32(0), nil
}
return Float32(math.Float32frombits(binary.BigEndian.Uint32(b))), nil
}
// Serialize implements the Type interface.
func (n Float32) Serialize() []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, math.Float32bits(float32(n)))
return b
}
// Len implements the Type interface.
func (n Float32) Len() int {
return 4
}
// Padding implements the Type interface.
func (n Float32) Padding() int {
return 0
}
// Type implements the Type interface.
func (n Float32) Type() TypeID {
return Float32Type
}
// String implements the Type interface.
func (n Float32) String() string {
return fmt.Sprintf("Float32{%0.4f}", n)
}