How to convert golang characters

PHPz
Release: 2023-04-14 11:19:47
Original
819 people have browsed it

When developing with Golang, we usually need to convert different types of data. Among them, character conversion is a common requirement. It can convert strings into corresponding ASCII code values, convert ASCII code values into corresponding characters, and so on.

This article will introduce the character conversion related operations in Golang, including ASCII code value to character, character to ASCII code value, Unicode code value to character, character to Unicode code value, etc.

  1. Conversion between ASCII code values and characters

In Golang, you can convert characters into corresponding ASCII code values, or you can convert ASCII code values into corresponding characters. The following is a sample code:

package main import "fmt" func main() { c := 'A' fmt.Println(int(c)) // 字符转 ASCII 码值 fmt.Println(string(65)) // ASCII 码值转字符 }
Copy after login

The above code converts the character 'A' into the corresponding ASCII code value, and the output result is 65; converts the ASCII code value 65 into the corresponding character 'A', and the output result is A.

  1. Conversion between Unicode code values and characters

Unicode is a character set that provides a standardized character encoding method for international programming. In Golang, you can convert Unicode code values into corresponding characters, and you can also convert characters into corresponding Unicode code values. The following is a sample code:

package main import "fmt" func main() { c := '中' fmt.Println(int(c)) // 字符转 Unicode 码值 fmt.Println(string(20013)) // Unicode 码值转字符 }
Copy after login

The above code converts the character '中' into the corresponding Unicode code value, and the output result is 20013; it converts the Unicode code value 20013 into the corresponding character '中', and the output result is middle.

It should be noted that Golang uses single quotes to represent characters and double quotes to represent strings. Characters occupy one byte (8 bits), while Unicode code values usually require two bytes (16 bits), so you need to pay attention to the correspondence between data types when converting.

  1. Other character conversion operations

In Golang, there are some other character conversion operations. For example, you can use the Atoi and Itoa functions of the strconv package to convert strings to integers or integers to strings; use the fmt.Sprintf function to convert formatted strings to strings; and so on.

Summary

This article introduces character conversion related operations in Golang, including ASCII code value to character, character to ASCII code value, Unicode code value to character, character to Unicode code value, etc. During the development process, we often need to convert different types of data. Mastering these operations can improve the efficiency of Golang development.

The above is the detailed content of How to convert golang characters. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!