当前位置:网站首页>Golang string (string) and byte array ([]byte) are converted to each other
Golang string (string) and byte array ([]byte) are converted to each other
2022-07-03 18:01:00 【Bald cat light King】
Golang character string (string) And byte arrays ([]byte) One line of code turns to another
Golang Easy to learnList of articles
One 、 String and byte array ?
The string is Go One of the most commonly used basic data types in the language , It is essentially a read-only character array , Although strings are often seen as a whole , But in fact, the string is a continuous memory space .
Go Another type of byte in language (Byte). stay ASCII in , An English letter takes up one byte of space , One Chinese character takes up two bytes of space . English punctuation takes up one byte , Chinese punctuation takes up two bytes . One Byte The elements in the array correspond to one ASCII code .
Two 、 Detailed code
1. Byte to string
The code is as follows ( Example ):
func BytesToString(data []byte) string {
return *(*string)(unsafe.Pointer(&data))
}
2. String to byte array
The code is as follows ( Example ):
func StringToBytes(data string) []byte {
return *(*[]byte)(unsafe.Pointer(&data))
}
3. Full running test
The code is as follows ( Example ):
func BytesToString(data []byte) string {
return *(*string)(unsafe.Pointer(&data))
}
func StringToBytes(data string) []byte {
return *(*[]byte)(unsafe.Pointer(&data))
}
func main() {
str := "hello world!"
fmt.Println(str)
a := StringToBytes(str)
fmt.Println(a)
b := BytesToString(a)
fmt.Println(b)
}
result ( Example ):
Successfully translated Hello world!
summary
There are two ways to remember that byte arrays and strings are interchangeable , Simple and direct , Practical pull full .
I hope this blog will be beneficial to you . I am the light king , I represent myself. .边栏推荐
- [combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
- Leetcode Valentine's Day Special - looking for a single dog
- Managing multiple selections with MVVM - managing multiple selections with MVVM
- Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
- Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
- As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
- 解决Zabbix用snmp监控网络流量不准的问题
- QT learning diary 9 - dialog box
- ArrayList分析3 : 删除元素
- supervisor监控Gearman任务
猜你喜欢
STM32 realizes 74HC595 control
Prototype inheritance..
聊聊支付流程的设计与实现逻辑
模块九作业
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
Leetcode Valentine's Day Special - looking for a single dog
Introduction to SolidWorks gear design software tool geartrax
面试官:值为 nil 为什么不等于 nil ?
Five problems of database operation in commodity supermarket system
Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
随机推荐
Talk about the design and implementation logic of payment process
[Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
Gear2021 monthly update - December
Leetcode 669 pruning binary search tree -- recursive method and iterative method
[Tongxin UOS] scanner device management driver installation
面试官:值为 nil 为什么不等于 nil ?
Win32: analyse du fichier dump pour la défaillance du tas
Design limitations of structure type (struct)
(8) HS corner detection
[LINUX]CentOS 7 安装MYSQL时报错“No package mysql-server available“No package zabbix-server-mysql availabl
Kotlin的協程:上下文
毕业总结
SDNUOJ1015
Embedded-c language-7
Talk about the design and implementation logic of payment process
Analyse ArrayList 3: suppression d'éléments
Image 24 bits de profondeur à 8 bits de profondeur
Automata and automatic line of non-standard design
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement