Commits

Cliff Dyer committed 3bedde6

Added functions for little endian conversions.

  • Participants
  • Parent commits b17b437

Comments (0)

Files changed (2)

+package endian
+
+func ToLittle(bytes [4]byte) uint32 {
+    return uint32(bytes[0]) + (uint32(bytes[1])<<8) + (uint32(bytes[2])<<16 + uint32(bytes[3])<<24)
+}
+
+func FromLittle(word uint32) [4]byte {
+    var bytes [4]byte
+    for i:=0; i<4; i++ {
+        bytes[i] = byte(word & 0xff)
+        word >>= 8
+    }
+    return bytes
+}

endian/littleendian_test.go

+package endian
+
+import "testing"
+import "fmt"
+
+   
+func TestConvertBytesToLittleEndian(t *testing.T) {
+    word := ToLittle([4]byte{0x33, 0x66, 0x99, 0xcc})
+    if word != 0xcc996633 {
+        fmt.Printf("%08x != 0xcc996633", word)
+        t.Fail()
+    }
+}
+
+func TestConvertWordFromLittleEndian(t *testing.T) {
+    bytes := FromLittle(0xcc996633)
+    fmt.Printf("%x != 0xcc996633", bytes)
+    if bytes != [4]byte{0x33, 0x66, 0x99, 0xcc} {
+        t.Fail()
+    }
+}