Multiple ways to Convert(cast) Binary to/from Decimal in Goland With example code
- Admin
- Dec 31, 2023
- Golang-examples
In these posts, You will learn two programs in the Go language.
- The first program is to convert
BinaryNumbers toDecimalNumbers - Second program to convert
DecimalNumbers toBinaryNumbers.
A binary number is a number based on base 2. It contains either zero or one digit. Each digit is called a bit.
Example binary numbers are 1011,10,01.
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 parses Binary to Decimal Numbers
There are 2 ways to convert binary numbers to decimal numbers.
- Using
strconv ParseIntfunction - Write conversion logic manually without using inbuilt functions
The below programs take input 1 number from a user console and store them in the variable of type binary.
How to convert Binary to a decimal using strconv ParseInt function golang?
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 my 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
How to convert Decimal to Binary Numbers in Golang
We have 2 ways to Convert Decimal to Binary numbers in Golang. The following are two ways
- using the
strconv FormatIntfunction - Manually conversion without using the Inbuilt function
The below program takes decimal input numbers from the user console and stores them in variable type decimal.
How to convert Decimal to Binary using golang strconv FormatInt function example
The 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
How to Cast Decimal to binary for loop without Golang inbuilt function
The following program uses Golang features such as 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.
