Home  >  Article  >  Backend Development  >  Use the strconv.FormatInt function to convert an integer into a string in the specified base

Use the strconv.FormatInt function to convert an integer into a string in the specified base

王林
王林Original
2023-07-24 10:51:291213browse

Use the strconv.FormatInt function to convert an integer into a string in a specified base number

In the Go language, the strconv package is a commonly used package for conversion between strings and other data types. . The strconv.FormatInt function can convert an integer into a string in a specified base. This article will introduce the use of strconv.FormatInt and provide some sample code.

First, let’s take a look at the signature of the strconv.FormatInt function:

func FormatInt(i int64, base int) string

This function accepts two parameters, i represents The integer to be converted, base represents the base number. The following base numbers can be used as base: 2, 8, 10, and 16. Among them, 2 represents binary, 8 represents octal, 10 represents decimal, and 16 represents hexadecimal.

The following is a simple example to convert a decimal integer into a binary string:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    i := 42
    binaryStr := strconv.FormatInt(int64(i), 2)
    fmt.Println(binaryStr) // 输出: "101010"
}

In the above code, we define an integer i and then use the strconv.FormatInt function Convert it to a binary string. Finally print the results.

Next, let’s look at an example of converting integers to other bases. The following code converts a decimal integer into a hexadecimal string:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    i := 255
    hexStr := strconv.FormatInt(int64(i), 16)
    fmt.Println(hexStr) // 输出: "ff"
}

In the above code, we convert a decimal integer i into a hexadecimal string. The result is "ff", which corresponds to 255 in decimal.

It should be noted that the strconv.FormatInt function returns a string type value. Depending on the length of the conversion result, you can choose to use int, int8, int16, int32, or int64 to store the converted integer value.

In addition, if you want to convert an integer to a string, you can use the strconv.Itoa function, which is similar to strconv.FormatInt.

To summarize, using the strconv.FormatInt function can easily convert an integer into a string in a specified base. By specifying different base numbers, we can convert integers into binary, octal, decimal or hexadecimal strings. This article provides some simple sample code to help readers better understand and use the strconv.FormatInt function.

The above is the detailed content of Use the strconv.FormatInt function to convert an integer into a string in the specified base. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn