From 047b82ff0144e7f77513858f5e6acbcebc73eb4f Mon Sep 17 00:00:00 2001 From: Jason Shiverick Date: Mon, 1 Mar 2021 22:02:08 -0800 Subject: [PATCH] Assign byte array directly to big.Int for big and little endian signals (#2) Co-authored-by: Naveen Venkatesan --- payload.go | 49 +++++++++----------------------------------- tests/decode_test.go | 5 +++++ 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/payload.go b/payload.go index 2ecd881..bc23a96 100644 --- a/payload.go +++ b/payload.go @@ -2,7 +2,6 @@ package can import ( "encoding/hex" - "fmt" "math/big" ) @@ -265,51 +264,22 @@ func (p *Payload) SetBit(i uint16, value bool) { // return packed // } +// PackLittleEndian packs the byte array into a continuous little endian big.Int func (p *Payload) PackLittleEndian() *big.Int { if p.PackedLittleEndian == nil { - // Initialize a big.Int called packed - //var packed, _ = new(big.Int).SetString(strings.Repeat("00000000", int(p.Length)), 2) - packed, _ := new(big.Int).SetString(CanBitsLittleEndian(p.Data), 2) - - // for i := 0; i < int(p.Length); i++ { - // //packed |= uint8(packed >> (i * 8)) - // fmt.Println(fmt.Sprintf("%08b", p.Data[i])) - // } - //packed.SetBytes(p.Data) + packed := new(big.Int).SetBytes(reverse(p.Data)) p.PackedLittleEndian = packed } return new(big.Int).Set(p.PackedLittleEndian) } -// CanBitsLittleEndian creates the zig zag pattern of bits feeding into a can.Data frame -func CanBitsLittleEndian(bytes []byte) string { - var bits string - for _, n := range bytes { - bn := fmt.Sprintf("%08b", n) - - // Need to reverse the binary strings because of the definition of bit order - bits += reverse(bn) - } - return reverse(bits) -} - -// CanBitsBigEndian creates the zig zag pattern of bits feeding into a can.Data frame -func CanBitsBigEndian(bytes []byte) string { - var bits string - for _, n := range bytes { - bn := fmt.Sprintf("%08b", n) - bits += bn - } - return bits -} - -// function to reverse strings -func reverse(s string) string { - runes := []rune(s) - for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { - runes[i], runes[j] = runes[j], runes[i] +// reverse byte array for little endian signals +func reverse(data []byte) []byte { + reversedArray := make([]byte, len(data)) + for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { + reversedArray[i], reversedArray[j] = data[j], data[i] } - return string(runes) + return reversedArray } // // PackBigEndian packs the data into a contiguous uint64 value for big-endian signals. @@ -326,9 +296,10 @@ func reverse(s string) string { // return packed // } +// PackBigEndian packs the byte array into a continuous big endian big.Int func (p *Payload) PackBigEndian() *big.Int { if p.PackedBigEndian == nil { - var packed, _ = new(big.Int).SetString(CanBitsBigEndian(p.Data), 2) + var packed = new(big.Int).SetBytes(p.Data) p.PackedBigEndian = packed } return new(big.Int).Set(p.PackedBigEndian) diff --git a/tests/decode_test.go b/tests/decode_test.go index aa84af8..a1f319b 100644 --- a/tests/decode_test.go +++ b/tests/decode_test.go @@ -203,11 +203,16 @@ func TestDecodeEACVariables(t *testing.T) { for _, signal := range message.Signals { value := signal.UnmarshalPhysicalPayload(&payload) + valueDesc, _ := signal.UnmarshalValueDescriptionPayload(&payload) name := signal.Name if value != expectedMap[name].value { t.Errorf("signal[%s] value = %f ; expected: %f", name, value, expectedMap[name].value) } + + if valueDesc != expectedMap[name].description { + t.Errorf("signal[%s] value = %s ; expected: %s", name, valueDesc, expectedMap[name].description) + } } }