Sling Academy
Home/Golang/Working with Octal and Hexadecimal Numbers in Go

Working with Octal and Hexadecimal Numbers in Go

Last updated: November 24, 2024

When working with different numerical systems in programming, you might encounter situations where you need to work with octal and hexadecimal numbers. These number systems are often used in computer science due to their compatibility with binary representations.

Understanding Octal and Hexadecimal

The octal system is base-8, which means it uses eight distinct symbols: 0 to 7. This system was used in early computers and is sometimes useful for representing file permissions in a shorthand form like in Unix systems.

The hexadecimal system is base-16, which uses sixteen symbols: 0 to 9 and the letters A to F. It is frequently used because two hex digits represent one byte (or 8 bits), making it more compact for binary data representation.

Working with Octal and Hexadecimal in Go

Let's look at how to use octal and hexadecimal numbers in the Go programming language, starting from the basics and moving towards more advanced techniques.

Basic: Using literals

In Go, you can directly use octal and hexadecimal literals. Octal numbers can be specified with a leading 0, while hexadecimal numbers use a leading 0x.

package main

import "fmt"

func main() {
    // Octal literal (base 8)
    octalNumber := 0757
    fmt.Println("Octal:", octalNumber)

    // Hexadecimal literal (base 16)
    hexNumber := 0x1A3F
    fmt.Println("Hexadecimal:", hexNumber)
}

Intermediate: Converting numbers

Sometimes you may need to convert numbers to and from octal and hexadecimal representations. Go's fmt package provides built-in functions to achieve this.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    num := 863

    // Convert decimal number to octal string
    octalStr := strconv.FormatInt(int64(num), 8)
    fmt.Println("Octal string:", octalStr)

    // Convert decimal number to hexadecimal string
    hexStr := strconv.FormatInt(int64(num), 16)
    fmt.Println("Hexadecimal string:", hexStr)
}

Advanced: Parsing strings to numbers

You might also need to parse octal or hexadecimal numbers from strings. This can be useful when processing input data or configuration options.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    octalStr := "0757"
    hexStr := "1A3F"

    // Parse octal string to decimal
    octalNum, err := strconv.ParseInt(octalStr, 8, 64)
    if err != nil {
        fmt.Println("Error converting octal:", err)
    } else {
        fmt.Println("Decimal from octal:", octalNum)
    }

    // Parse hexadecimal string to decimal
    hexNum, err := strconv.ParseInt(hexStr, 16, 64)
    if err != nil {
        fmt.Println("Error converting hexadecimal:", err)
    } else {
        fmt.Println("Decimal from hexadecimal:", hexNum)
    }
}

With these examples, you should now have a solid understanding of dealing with octal and hexadecimal numbers in Go. As you continue to build your applications, these tools can be essential for certain situations involving data representation and manipulation.

Next Article: Formatting Numbers for Output in Binary, Hex, and Decimal in Go

Previous Article: Exploring Binary Representation of Numbers 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