Skip to content

Commit b83eec4

Browse files
committed
Change LittleEndian to BigEndian encoding
1 parent 048b49f commit b83eec4

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

encoding.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,30 @@ func encodeKeyOffset(key []byte, offset int, w io.Writer) (int, error) {
9393
// Must be compatible with decodeInt.
9494
func encodeInt(x int) []byte {
9595
var encoded [8]byte
96-
binary.LittleEndian.PutUint64(encoded[:], uint64(x))
96+
binary.BigEndian.PutUint64(encoded[:], uint64(x))
9797

9898
return encoded[:]
9999
}
100100

101101
// decodeInt decodes the slice of bytes as an int.
102102
// Must be compatible with encodeInt.
103103
func decodeInt(encoded []byte) int {
104-
return int(binary.LittleEndian.Uint64(encoded))
104+
return int(binary.BigEndian.Uint64(encoded))
105105
}
106106

107107
// encodeIntPair encodes two ints.
108108
func encodeIntPair(x, y int) []byte {
109109
var encoded [16]byte
110-
binary.LittleEndian.PutUint64(encoded[0:8], uint64(x))
111-
binary.LittleEndian.PutUint64(encoded[8:], uint64(y))
110+
binary.BigEndian.PutUint64(encoded[0:8], uint64(x))
111+
binary.BigEndian.PutUint64(encoded[8:], uint64(y))
112112

113113
return encoded[:]
114114
}
115115

116116
// decodeIntPair decodes two ints.
117117
func decodeIntPair(encoded []byte) (int, int) {
118-
x := int(binary.LittleEndian.Uint64(encoded[0:8]))
119-
y := int(binary.LittleEndian.Uint64(encoded[8:]))
118+
x := int(binary.BigEndian.Uint64(encoded[0:8]))
119+
y := int(binary.BigEndian.Uint64(encoded[8:]))
120120

121121
return x, y
122122
}

encoding_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestEncodePut(t *testing.T) {
1515
}
1616

1717
// total = 14, key = 3, key and value
18-
expected := []byte{14, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6}
18+
expected := []byte{0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3, 4, 5, 6}
1919
if !bytes.Equal(expected, buffer.Bytes()) {
2020
t.Fatalf("failed to encoded key/value, expected %v, but received %v", expected, buffer.Bytes())
2121
}
@@ -30,14 +30,14 @@ func TestEncodeDelete(t *testing.T) {
3030
}
3131

3232
// total = 11, key = 3, key and value
33-
expected := []byte{11, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3}
33+
expected := []byte{0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3}
3434
if !bytes.Equal(expected, buffer.Bytes()) {
3535
t.Fatalf("failed to encode key/value, expected %v, but received %v", expected, buffer.Bytes())
3636
}
3737
}
3838

3939
func TestDecodePut(t *testing.T) {
40-
data := []byte{14, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6}
40+
data := []byte{0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3, 4, 5, 6}
4141
buffer := bytes.NewBuffer(data)
4242

4343
key, value, err := decode(buffer)
@@ -55,7 +55,7 @@ func TestDecodePut(t *testing.T) {
5555
}
5656

5757
func TestDecodeDelete(t *testing.T) {
58-
data := []byte{11, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3}
58+
data := []byte{0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3}
5959
buffer := bytes.NewBuffer(data)
6060

6161
key, value, err := decode(buffer)
@@ -115,4 +115,4 @@ func TestEncodeDeleteDecode(t *testing.T) {
115115
if nil != decodedValue {
116116
t.Fatalf("failed to encode/decode value, expected %v, but received %v", nil, decodedValue)
117117
}
118-
}
118+
}

0 commit comments

Comments
 (0)