{

Multiple ways to to Convert(cast) Binary to/from Decimal in Goland With example code


Golang example Convert(cast) Binary to/from Decimal

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,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 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 1 number from a user console, and store them in variable of type 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 type 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 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.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.