Skip to content

Commit 0dca354

Browse files
committed
add helper functions in js utils
Signed-off-by: Jeeva Kandasamy <jkandasa@gmail.com>
1 parent 8905500 commit 0dca354

File tree

2 files changed

+146
-6
lines changed

2 files changed

+146
-6
lines changed

pkg/utils/javascript/js_helper/convert.go

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,68 @@ func (ll *Convert) ToStringFromBase64(data interface{}) string {
5151
return string(bytes)
5252
}
5353

54+
// ToUInt16LE converts a 2-byte little-endian slice to a uint16.
5455
func (ll *Convert) ToUInt16LE(bytes []byte) uint16 {
5556
return binary.LittleEndian.Uint16(bytes)
5657
}
5758

59+
// ToUInt32LE converts a 4-byte little-endian slice to a uint32.
60+
func (ll *Convert) ToUInt32LE(bytes []byte) uint32 {
61+
return binary.LittleEndian.Uint32(bytes)
62+
}
63+
64+
// ToUInt64LE converts an 8-byte little-endian slice to a uint64.
65+
func (ll *Convert) ToUInt64LE(bytes []byte) uint64 {
66+
return binary.LittleEndian.Uint64(bytes)
67+
}
68+
69+
// ToInt16LE converts a 2-byte little-endian slice to an int16,
70+
// preserving the sign using two's complement representation.
5871
func (ll *Convert) ToInt16LE(bytes []byte) int16 {
59-
ref := ll.ToUInt16LE(bytes)
60-
if ref > 0x7fff {
61-
return int16(-65536 + int(ref))
62-
} else {
63-
return int16(ref)
64-
}
72+
return int16(binary.LittleEndian.Uint16(bytes))
6573
}
6674

75+
// ToInt32LE converts a 4-byte little-endian slice to an int32,
76+
// preserving the sign using two's complement representation.
77+
func (ll *Convert) ToInt32LE(bytes []byte) int32 {
78+
return int32(binary.LittleEndian.Uint32(bytes))
79+
}
80+
81+
// ToInt64LE converts an 8-byte little-endian slice to an int64,
82+
// preserving the sign using two's complement representation.
83+
func (ll *Convert) ToInt64LE(bytes []byte) int64 {
84+
return int64(binary.LittleEndian.Uint64(bytes))
85+
}
86+
87+
// ToUInt16BE converts a 2-byte big-endian slice to a uint16.
6788
func (ll *Convert) ToUInt16BE(bytes []byte) uint16 {
6889
return binary.BigEndian.Uint16(bytes)
6990
}
91+
92+
// ToUInt32BE converts a 4-byte big-endian slice to a uint32.
93+
func (ll *Convert) ToUInt32BE(bytes []byte) uint32 {
94+
return binary.BigEndian.Uint32(bytes)
95+
}
96+
97+
// ToUInt64BE converts an 8-byte big-endian slice to a uint64.
98+
func (ll *Convert) ToUInt64BE(bytes []byte) uint64 {
99+
return binary.BigEndian.Uint64(bytes)
100+
}
101+
102+
// ToInt16BE converts a 2-byte big-endian slice to an int16,
103+
// preserving the sign using two's complement representation.
104+
func (ll *Convert) ToInt16BE(bytes []byte) int16 {
105+
return int16(binary.BigEndian.Uint16(bytes))
106+
}
107+
108+
// ToInt32BE converts a 4-byte big-endian slice to an int32,
109+
// preserving the sign using two's complement representation.
110+
func (ll *Convert) ToInt32BE(bytes []byte) int32 {
111+
return int32(binary.BigEndian.Uint32(bytes))
112+
}
113+
114+
// ToInt64BE converts an 8-byte big-endian slice to an int64,
115+
// preserving the sign using two's complement representation.
116+
func (ll *Convert) ToInt64BE(bytes []byte) int64 {
117+
return int64(binary.BigEndian.Uint64(bytes))
118+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package javascript_helper
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestEndianConversions(t *testing.T) {
8+
conv := &Convert{}
9+
10+
// test data
11+
leBytes16 := []byte{0x34, 0x12} // 0x1234
12+
leBytes32 := []byte{0x78, 0x56, 0x34, 0x12} // 0x12345678
13+
leBytes64 := []byte{0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12} // 0x123456789ABCDEF0
14+
15+
beBytes16 := []byte{0x12, 0x34}
16+
beBytes32 := []byte{0x12, 0x34, 0x56, 0x78}
17+
beBytes64 := []byte{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0}
18+
19+
t.Run("ToUInt16LE", func(t *testing.T) {
20+
want := uint16(0x1234)
21+
got := conv.ToUInt16LE(leBytes16)
22+
if got != want {
23+
t.Errorf("ToUInt16LE() = %d, want %d", got, want)
24+
}
25+
})
26+
27+
t.Run("ToUInt32LE", func(t *testing.T) {
28+
want := uint32(0x12345678)
29+
got := conv.ToUInt32LE(leBytes32)
30+
if got != want {
31+
t.Errorf("ToUInt32LE() = %d, want %d", got, want)
32+
}
33+
})
34+
35+
t.Run("ToUInt64LE", func(t *testing.T) {
36+
want := uint64(0x123456789ABCDEF0)
37+
got := conv.ToUInt64LE(leBytes64)
38+
if got != want {
39+
t.Errorf("ToUInt64LE() = %d, want %d", got, want)
40+
}
41+
})
42+
43+
t.Run("ToUInt16BE", func(t *testing.T) {
44+
want := uint16(0x1234)
45+
got := conv.ToUInt16BE(beBytes16)
46+
if got != want {
47+
t.Errorf("ToUInt16BE() = %d, want %d", got, want)
48+
}
49+
})
50+
51+
t.Run("ToUInt32BE", func(t *testing.T) {
52+
want := uint32(0x12345678)
53+
got := conv.ToUInt32BE(beBytes32)
54+
if got != want {
55+
t.Errorf("ToUInt32BE() = %d, want %d", got, want)
56+
}
57+
})
58+
59+
t.Run("ToUInt64BE", func(t *testing.T) {
60+
want := uint64(0x123456789ABCDEF0)
61+
got := conv.ToUInt64BE(beBytes64)
62+
if got != want {
63+
t.Errorf("ToUInt64BE() = %d, want %d", got, want)
64+
}
65+
})
66+
67+
t.Run("ToInt16LE", func(t *testing.T) {
68+
bytes := []byte{0xFF, 0x7F} // 0x7FFF -> 32767
69+
want := int16(32767)
70+
got := conv.ToInt16LE(bytes)
71+
if got != want {
72+
t.Errorf("ToInt16LE() = %d, want %d", got, want)
73+
}
74+
75+
bytes = []byte{0x00, 0x80} // 0x8000 -> -32768
76+
want = -32768
77+
got = conv.ToInt16LE(bytes)
78+
if got != want {
79+
t.Errorf("ToInt16LE() = %d, want %d", got, want)
80+
}
81+
})
82+
83+
t.Run("ToInt32BE", func(t *testing.T) {
84+
bytes := []byte{0xFF, 0xFF, 0xFF, 0xFF} // -1
85+
want := int32(-1)
86+
got := conv.ToInt32BE(bytes)
87+
if got != want {
88+
t.Errorf("ToInt32BE() = %d, want %d", got, want)
89+
}
90+
})
91+
}

0 commit comments

Comments
 (0)