Home  >  Article  >  Backend Development  >  golang base conversion class

golang base conversion class

王林
王林Original
2023-05-10 11:47:06818browse

Golang is an open source programming language. It is increasingly favored by programmers for its efficiency, simplicity, security and cross-platform features. Compared to other programming languages, Golang has powerful and flexible libraries and tools, which makes it a very convenient programming language to use.

In Golang, base conversion is one of the common operations. Developers often need to convert a number from one base to another, such as converting binary to decimal or hexadecimal. In this article, we will explore how to implement the base conversion class method in Golang.

1. Convert decimal to other bases

  1. Binary

In Golang, you can use the fmt.Sprintf method to convert decimal to binary. For example, the following code will convert the number 250 to a binary number:

str := fmt.Sprintf("%b", 250) 

"%b" is a placeholder that indicates that the number is to be converted to a binary string. The value of the "str" ​​variable returned by this method is "11111010".

  1. Octal

Converting decimal to octal is also very simple. In Golang, this can be achieved using the "%o" placeholder. For example, the following code converts the number 250 to octal:

str := fmt.Sprintf("%o", 250) 

The value of the "str" ​​variable returned by this method is "372".

  1. HEX

Converting a decimal number to a hexadecimal number is also easy. In Golang, this can be achieved using the "%x" placeholder. For example, the following code converts the number 250 to hexadecimal:

str := fmt.Sprintf("%x", 250) 

The value of the "str" ​​variable returned by this method is "fa".

2. Convert other bases to decimal

  1. Binary

It is also easy to convert a binary number into a decimal number. In Golang, this can be achieved using the strconv.ParseInt function. For example, the following code converts the binary string "11111010" into a decimal number:

num, err := strconv.ParseInt("11111010", 2, 64) 

The first parameter in this function is the string to be converted, and the second parameter is the number to be converted. The third parameter indicates the type of returned result.

  1. Octal

Converting an octal number to a decimal number is also very simple. In Golang, this can be achieved using the strconv.ParseInt function. For example, the following code converts the octal string "372" into a decimal number:

num, err := strconv.ParseInt("372", 8, 64) 

In this function, the first parameter is the string to be converted, and the second parameter is the number to be converted. The third parameter indicates the type of returned result.

  1. HEX

Converting hexadecimal numbers to decimal numbers is also easy. In Golang, this can be achieved using the strconv.ParseInt function. For example, the following code converts the hexadecimal string "fa" into a decimal number:

num, err := strconv.ParseInt("fa", 16, 64) 

In this function, the first parameter is the string to be converted, and the second parameter is the string to be converted. The conversion base, the third parameter indicates the type of the returned result.

3. Summary

In Golang, base conversion is a very common operation. We can use the fmt.Sprintf and strconv.ParseInt functions to implement these conversions. When converting decimal to other bases, you can use the placeholders "%b", "%o" and "%x". When converting other bases to decimal, you only need to add the corresponding base number and the character to be converted. Just pass the string to the strconv.ParseInt function. It should be noted that when using these methods, error exceptions may be thrown for unsupported bases.

Although hexadecimal conversion seems simple, in actual application, some unpredictable errors may occur. Therefore, we recommend that when writing Golang code, you should always be careful and test the code to the maximum extent.

The above is the detailed content of golang base conversion class. 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