Use the strconv.FormatInt function to convert an integer into a string in the specified base and return an error message
In the Go language, the strconv package provides a series of functions for strings and basic data types. conversion between. The strconv.FormatInt function can convert an integer according to the specified base and return the corresponding string representation. Let's take a closer look at how to use this function.
The function signature is as follows:
func FormatInt(i int64, base int) string
Among them, i represents the integer to be converted, base represents the base to be converted, and the return value is the converted string.
Next we look at an example to convert an integer into a hexadecimal string and output the result.
package main import ( "fmt" "strconv" ) func main() { num := 255 // 将num转换为16进制字符串 str := strconv.FormatInt(int64(num), 16) fmt.Println("转换结果:", str) }
The output result is:
Conversion result: ff
In this example, we convert an integer 255 into the hexadecimal string "ff", and Print the results. As you can see, we use the strconv.FormatInt function to convert the integer num into a hexadecimal string.
In addition to converting to hexadecimal, we can also convert integers to strings in other bases. Just set the base parameter to the corresponding base. For example, convert integers to binary, octal, etc.
The following is a sample code to convert an integer to octal:
package main import ( "fmt" "strconv" ) func main() { num := 255 // 将num转换为8进制字符串 str := strconv.FormatInt(int64(num), 8) fmt.Println("转换结果:", str) }
The output result is:
Conversion result: 377
The above code converts the integer 255 Convert to the string "377" in octal.
When using the strconv.FormatInt function, you may encounter some error conditions, such as the conversion value exceeds the range. At this time, the function will return a non-nil error value. We can handle exceptions by judging error values. Below is a sample code that demonstrates how to handle error conditions.
package main import ( "fmt" "strconv" ) func main() { num := 255 // 将num转换为二进制字符串 str, err := strconv.FormatInt(int64(num), 2) if err != nil { fmt.Println("转换错误:", err) return } fmt.Println("转换结果:", str) }
The output result is:
Conversion result: 11111111
As you can see, we use a variable err to receive the error value. If an error occurs during the conversion process, err will not be nil, and we can handle the corresponding error by processing err.
To summarize, use the strconv.FormatInt function to convert integers according to the specified base and return the corresponding string representation. We can set different bases by modifying the base parameter to complete the conversion operation of integers. At the same time, be sure to pay attention to handling error situations to avoid unpredictable problems in the program.
The above is the detailed content of Use the strconv.FormatInt function to convert an integer into a string in the specified base and return an error message. For more information, please follow other related articles on the PHP Chinese website!