Multiple ways to Convert(cast) Octal to Decimal Number in Golang

In this blog post, You will learn two programs in the Go language.

  • The first program is to convert Octal to a Decimal Number
  • The second program is to convert Decimal to Octal Number.

What is the Octal Number in Golang?

An Octal number is a number based on the base 8 number system. It contains digits from 0 to 7. In Golang, Octal numbers are always prefixed with zero. An example octal number is 0879.

Decimal Numbers are numbers that contain numbers based on base 10.

Example Program to convert Octal to Decimal Number

We have 2 ways to Convert Octal to Decimal Numbers in Go language. The following are the ways.

  • Using strconv ParseInt function
  • Custom Function using for loop without inbuilt functions

The below program takes Octal numbers (prefixed with zero or the normal number for Declaring Octal numbers) and stored them in variable octal.

How to convert Octal to Decimal using golang strconv ParseInt function example

The strconv package provides the ParseInt function. To convert 1 number, ParseInt is provided with the number and base=8.

Here is an example program for conversion to Decimal Numbers

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var octal string
    fmt.Print("Enter Octal Number:")
    fmt.Scanln( & octal)
    output, err: = strconv.ParseInt(octal, 8, 64)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Printf("Output %d", output)
}

When the above program is compiled, the Output is

Enter Octal Number:045
Output 37

How to convert Octal to Decimal using golang Custom Function without Predefined functions

Created a user-defined function, inside a custom function, used for loop with Modulus, Division operator, and Math pow function.

Here is an example program

package main

import (
    "fmt"
    "math"
)

func convertOctaToDecimal(number int) int {
    decimal: = 0
    counter: = 0.0
    remainder: = 0

        for number != 0 {
        remainder = number % 10
        decimal += remainder * int(math.Pow(8.0, counter))
        number = number / 10
        counter++
    }
    return decimal
}

func main() {
    var octal int
    fmt.Print("Enter Octal Number:")
    fmt.Scanln( & octal)

    fmt.Printf("Output %d", convertOctaToDecimal(octal))

}

Output:

Enter Octal Number:25
Output 21

Golang converts Decimal to Octal Number examples

We have 2 ways to convert Decimal to Octal numbers in Golang.
Below are two ways

  • using strconv FormatInt function
  • Without Inbuilt Function

The below program takes decimal input numbers from the keyboard console, and stores them in the variable decimal.

How to convert Decimal to Octal using strconv FormatInt function

The inbuilt Standard package strconv provides the FormatInt function used to convert Decimal to Octal numbers.
Here is an example program for Casting Decimal to Octal

package main

import (
    "fmt"
    "math"
    "strconv"
)
func main() {
    var decimal int64
    fmt.Print("Enter Decimal Number:")
    fmt.Scanln( & decimal)
    output: = strconv.FormatInt(decimal, 8)
    fmt.Print("Output ", output)

}

The output of the compiled program is

Enter Decimal Number:124
Output 174

How to convert an octal to a decimal using the golang Custom function without inbuilt functionality

The following program uses Golang features For loop, Modulus, and division operators.
Here is an example program to do the conversion.

package main

import (
    "fmt"
)

func convertDecimalToOctal(number int) int {
    octal: = 0
    counter: = 1
    remainder: = 0
    for number != 0 {
        remainder = number % 8
        number = number / 8
        octal += remainder * counter
        counter *= 10
    }
    return octal
}

func main() {
    var decimal int
    fmt.Print("Enter Decimal Number:")
    fmt.Scanln( & decimal)
    fmt.Print("Output ", convertDecimalToOctal(decimal))

}

Output:

Enter Decimal Number:145
Output 221