Golang : Convert integer to binary, octal, hexadecimal and back to integer
Converting integers to binary, octal and hexadecimal is fairly common in programming life. Ported these simple functions from Python to convert integer to binary, octal and hexadecimal. Hope these functions can be useful to you.
Here you go!
package main
import (
"fmt"
"strconv"
)
func bin(i int, prefix bool) string {
i64 := int64(i)
if prefix {
return "0b" + strconv.FormatInt(i64, 2) // base 2 for binary
} else {
return strconv.FormatInt(i64, 2) // base 2 for binary
}
}
func bin2int(binStr string) int {
// base 2 for binary
result, _ := strconv.ParseInt(binStr, 2, 64)
return int(result)
}
func oct(i int, prefix bool) string {
i64 := int64(i)
if prefix {
return "0o" + strconv.FormatInt(i64, 8) // base 8 for octal
} else {
return strconv.FormatInt(i64, 8) // base 8 for octal
}
}
func oct2int(octStr string) int {
// base 8 for octal
result, _ := strconv.ParseInt(octStr, 8, 64)
return int(result)
}
func hex(i int, prefix bool) string {
i64 := int64(i)
if prefix {
return "0x" + strconv.FormatInt(i64, 16) // base 16 for hexadecimal
} else {
return strconv.FormatInt(i64, 16) // base 16 for hexadecimal
}
}
func hex2int(hexStr string) int {
// base 16 for hexadecimal
result, _ := strconv.ParseInt(hexStr, 16, 64)
return int(result)
}
func main() {
num := 123456789
fmt.Println("Integer : ", num)
fmt.Println("Binary : ", bin(num, false))
fmt.Println("Octal : ", oct(num, true))
fmt.Println("Hex : ", hex(num, true))
// bin2int function does not handle the prefix
// so set second parameter to false
// otherwise you will get funny result
fmt.Println("Binary to Integer : ", bin2int(bin(num, false)))
fmt.Println("Octal to Integer : ", oct2int(oct(num, false)))
fmt.Println("Hexadecimal to Integer : ", hex2int(hex(num, false)))
}
Output:
Integer : 123456789
Binary : 111010110111100110100010101
Octal : 0o726746425
Hex : 0x75bcd15
Binary to Integer : 123456789
Octal to Integer : 123456789
Hexadecimal to Integer : 123456789
and if you are looking for super quick way to convert integer to binary, octal or hexadecimal variables. See this example:
package main
import (
"fmt"
)
func main() {
num := int64(123456789)
bin := fmt.Sprintf("%b", num)
fmt.Println(bin)
oct := fmt.Sprintf("%o", num)
fmt.Println(oct)
hex := fmt.Sprintf("%x", num)
fmt.Println(hex)
}
Output:
111010110111100110100010101
726746425
75bcd15
Happy coding!
Reference:
See also : Golang : Convert octal value to string to deal with leading zero problem
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.1k Python : Create Whois client or function example
+13.8k Golang : Get dimension(width and height) of image file
+11k How to test Facebook App on localhost ?
+23.7k Golang : Fix type interface{} has no field or no methods and type assertions example
+8.1k Golang : Find relative luminance or color brightness
+15.1k Golang : Get HTTP protocol version example
+15.5k Golang : How to convert(cast) IP address to string?
+7.2k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+24.3k Golang : How to validate URL the right way
+15.1k Golang : Save(pipe) HTTP response into a file
+10.1k Golang : How to get quoted string into another string?
+15.5k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content