Skip to content

Commit

Permalink
fix BINToInt and IntToBIN
Browse files Browse the repository at this point in the history
  • Loading branch information
LLLLimbo committed Jul 7, 2023
1 parent 1e226d9 commit cb2c507
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,27 @@ func Cp56time2aToUnixMilliseconds(cp56time2a []byte) int64 {
return t.UnixNano() / int64(time.Millisecond)
}

func IntToBIN(v int, len int) []byte {
func IntToBIN(v int, l int) []byte {
value := uint32(v)
b := make([]byte, len)
var b []byte
if l < 4 {
b = make([]byte, 4)
} else {
b = make([]byte, l)
}

binary.LittleEndian.PutUint32(b, value)

if l < len(b) {
b = b[:l]
}
return b
}

func BINToInt(b []byte) int {
for len(b) < 4 {
b = append(b, 0x00)
}
value := binary.LittleEndian.Uint32(b)
return int(value)
}

0 comments on commit cb2c507

Please sign in to comment.