Converting between binary, octal, decimal, and hexadecimal
In some cases, the integer values can be represented by other than decimal representations. The conversion between these representations is easily done with the use of the strconv
package.
How to do it...
- Open the console and create the folder
chapter03/recipe06
. - Navigate to the directory.
- Create the
convert.go
file with the following content:
package main import ( "fmt" "strconv" ) const bin = "10111" const hex = "1A" const oct = "12" const dec = "10" const floatNum = 16.123557 func main() { // Converts binary value into hex v, _ := ConvertInt(bin, 2, 16) fmt.Printf("Binary value %s converted to hex: %s\n", bin, v) // Converts hex value into dec v, _ = ConvertInt(hex, 16, 10) fmt.Printf("Hex value %s converted to dec: %s\n", hex, v) // Converts oct value into hex ...