Go language is an open source programming language that can be used to quickly develop simple and reliable programs. In the development process of Go language, hexadecimal conversion is often required. This article will explain how to perform base conversion in Go language.
Decimal to Binary
In Go language, you can use the strconv package to convert decimal numbers to binary numbers.
package main import ( "fmt" "strconv" ) func main() { num := 10 binary := strconv.FormatInt(int64(num), 2) fmt.Println(num, "的二进制表示为", binary) }
The above code will output:
10 的二进制表示为 1010
The strconv.FormatInt() function converts a decimal number into a binary string. The first parameter of this function is the value to be converted, and the second parameter is the target base, which is 2 in this example.
Binary to Decimal
If you need to convert a binary value to a decimal value, you can also use the strconv package.
package main import ( "fmt" "strconv" ) func main() { binary := "1010" num, _ := strconv.ParseInt(binary, 2, 64) fmt.Println(binary, "的十进制表示为", num) }
The above code will output:
1010 的十进制表示为 10
strconv.ParseInt() function parses a string into an integer. The first parameter of this function is the string to be parsed, the second parameter is the target base, in this case it is 2, and the third parameter is the number of digits in the result type, in this case it is a 64-bit integer.
Decimal to hexadecimal
In the Go language, the strconv package also provides a function for converting decimal numbers to hexadecimal numbers:
package main import ( "fmt" "strconv" ) func main() { num := 33752069 hex := strconv.FormatInt(int64(num), 16) fmt.Println(num, "的十六进制表示为", hex) }
The above code Will output:
33752069 的十六进制表示为 2030405
strconv.FormatInt() function converts a decimal number into a hexadecimal string. The first parameter of this function is the value to be converted, and the second parameter is the target base, which is 16 in this example.
Hexadecimal to Decimal
If you need to convert a hexadecimal value to a decimal value, you can also use the strconv package.
package main import ( "fmt" "strconv" ) func main() { hex := "2030405" num, _ := strconv.ParseInt(hex, 16, 64) fmt.Println(hex, "的十进制表示为", num) }
The above code will output:
2030405 的十进制表示为 33752069
strconv.ParseInt() function parses a string into an integer. The first parameter of this function is the string to be parsed, the second parameter is the target base, in this case it is 16, and the third parameter is the number of digits in the result type, in this case it is a 64-bit integer.
Summary
This article introduces how to perform base conversion in Go language. Through the strconv package, we can easily convert decimal numbers to other base numbers, or other base numbers to decimal numbers. This is one of the very common skills in Go language programming.
The above is the detailed content of How to perform hexadecimal conversion in Go language. For more information, please follow other related articles on the PHP Chinese website!