
In these posts, You will learn two programs in the Go language.
- The first program is to convert Binary Numbers to Decimal Numbers
- Second program to convert Decimal Numbers to Binary Number.
A binary number
is a number based on base 2. It contains either zero or one digits. Each digit is called a bit.
Example binary numbers are 1011
Decimal Numbers
are numbers that contain numbers based on base 10.
Example decimal numbers are 12 and 44.
To understand this example, You should have the following features in the Go language.
- Golang For Loop control structure
- Golang For Loop Range form
- Golang Operator Guide
- Golang Datatype guide
- Beginner Guide to Functions with examples
Golang parse Binary to Decimal Number
There are 2 ways to convert binary numbers to decimal numbers.
- Using strconv ParseInt function
- Write conversion logic manually without using inbuilt functions
The below programs takes input binary number from a user console, and store them in variable binary.
How to convert Binary to a decimal using strconv ParseInt function?
The strconv
package provides the ParseInt
function used to convert binary to decimal numbers.
Here is an example program
package main
import (
"fmt"
"strconv"
)
func main() {
var binary string
fmt.Print("Enter Binary Number:")
fmt.Scanln(&binary)
output, err := strconv.ParseInt(binary, 2, 64)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Output %d", output)
}
Output:
Enter Binary Number:1111
Output 15
Manual Custom function to Cast Binary To decimal using for loop
Created an own function, inside a function, used for loop
with Modulus and Division operator and Math pow function.
Here is an example program
package main
import (
"fmt"
"math"
)
func convertBinaryToDecimal(number int) int {
decimal := 0
counter := 0.0
remainder := 0
for number != 0 {
remainder = number % 10
decimal += remainder * int(math.Pow(2.0, counter))
number = number / 10
counter++
}
return decimal
}
func main() {
var binary int
fmt.Print("Enter Binary Number:")
fmt.Scanln(&binary)
fmt.Printf("Output %d", convertBinaryToDecimal(binary))
}
Output:
Enter Binary Number:1001
Output 9
Example Program to convert Decimal to Binary Number
We have 2 ways to Convert Decimal to Binary numbers in Golang. The following are two ways
- using strconv FormatInt function
- Manually conversion without using Inbuilt function
The below program takes decimal input numbers from the user console and stores them in variable decimal.
strconv FormatInt function example to cast Decimal to Binary
Inbuilt Standard package strconv
provides the FormatInt
function used to Convert Decimal to Binary numbers.
Here is an example program
package main
import (
"fmt"
"strconv"
)
func main() {
var decimal int64
fmt.Print("Enter Decimal Number:")
fmt.Scanln(&decimal)
output := strconv.FormatInt(decimal, 2)
fmt.Print("Output ", output)
}
Output:
Enter Decimal Number:15
Output 1111
Cast Decimal to binary for loop without inbuilt function
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 convertDecimalToBinary(number int) int {
binary := 0
counter := 1
remainder := 0
for number != 0 {
remainder = number % 2
number = number / 2
binary += remainder * counter
counter *= 10
}
return binary
}
func main() {
var decimal int
fmt.Print("Enter Decimal Number:")
fmt.Scanln(&decimal)
fmt.Printf("Output %d", convertDecimalToBinary(decimal))
}
Output:
Enter Decimal Number:15
Output 1111
Conclusion
You learned how to convert multiple ways to convert decimal to binary and binary to decimal with an example program.