Sling Academy
Home/Golang/Working with Endianness: Little vs Big Endian Conversions in Go

Working with Endianness: Little vs Big Endian Conversions in Go

Last updated: November 24, 2024

Endianness is a fundamental concept in computer science and can be critical when solving problems related to data storage and transmission. In this article, we’ll explore how to work with endianness in the Go programming language, focusing on conversions between little endian and big endian formats.

Understanding Endianness

Endianness refers to the sequence of bytes used to represent data types (like integers) in computer memory. The two most common are:

  • Big Endian: The most significant byte (MSB) is stored at the smallest memory address (the "front" of the arrangement).
  • Little Endian: The least significant byte (LSB) is stored at the smallest memory address.

Different systems use different endianness, and Go provides package functions to help handle these.

Basic Byte Conversion in Go Using encoding/binary

Go offers the encoding/binary package, which allows easy reading and writing of streams of binary numbers. Below are examples of handling integer numbers with different endian formats.

Basic Example: Converting a Uint32

package main

import (
    "encoding/binary"
    "fmt"
)

func main() {
    // 123456789 is an arbitrary integer example.
    var num uint32 = 123456789
    fmt.Printf("Original number in Hex: %x\n", num)

    // Convert to Little Endian
    var littleEndian = make([]byte, 4)
    binary.LittleEndian.PutUint32(littleEndian, num)
    fmt.Printf("Little Endian: % x\n", littleEndian)

    // Convert to Big Endian
    var bigEndian = make([]byte, 4)
    binary.BigEndian.PutUint32(bigEndian, num)
    fmt.Printf("Big Endian: % x\n", bigEndian)
}

Intermediate Example: Reading Integers from a Byte Slice

package main

import (
    "encoding/binary"
    "fmt"
)

func main() {
    // Example byte slice for Little Endian number
    littleEndianBytes := []byte{0x15, 0xCD, 0x5B, 0x07} // represents 123456789
    littleNum := binary.LittleEndian.Uint32(littleEndianBytes)
    fmt.Printf("Little Endian to uint32: %d\n", littleNum)

    // Example byte slice for Big Endian number
    bigEndianBytes := []byte{0x07, 0x5B, 0xCD, 0x15} // also represents 123456789
    bigNum := binary.BigEndian.Uint32(bigEndianBytes)
    fmt.Printf("Big Endian to uint32: %d\n", bigNum)
}

Advanced Calculation: Operating on Multibyte Sequences

When dealing with complex data structures, converting entire structs or multiple-byte sequences is essential, especially in network programming.

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
)

type Header struct {
    ID   uint16
    Len  uint16
}

func main() {
    // Create a Header instance
    header := Header{ID: 1, Len: 674}
    fmt.Printf("Header: %+v\n", header)

    // Convert struct to a byte buffer in Little Endian
    buf := new(bytes.Buffer)
    binary.Write(buf, binary.LittleEndian, header)
    fmt.Printf("Little Endian Buffer: % x\n", buf.Bytes())

    // Reset buffer and convert to and from Big Endian
    buf.Reset()
    binary.Write(buf, binary.BigEndian, header)
    fmt.Printf("Big Endian Buffer: % x\n", buf.Bytes())
}

Understanding and manipulating byte order using endianness is vital in software dealing with data communication, storage, and formats abiding to a specific binary structure.

Next Article: Converting Binary Data to Structs with Go's `encoding/binary` Package

Previous Article: Encoding Numbers in Base64 and Decoding Them in Go

Series: Numbers and Math in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant