当前位置:网站首页>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. .边栏推荐
- A. Odd Selection【BruteForce】
- SDNUOJ1015
- [enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
- This diversion
- TCP拥塞控制详解 | 3. 设计空间
- Automata and automatic line of non-standard design
- MinGW compile boost library
- MySQL has been stopped in the configuration interface during installation
- 统计图像中各像素值的数量
- STM32实现74HC595控制
猜你喜欢

Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028

Leetcode 108 converts an ordered array into a binary search tree -- recursive method
![[combinatorics] generating function (summation property)](/img/74/e6ef8ee69ed07d62df9f213c015f2c.jpg)
[combinatorics] generating function (summation property)

Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries

Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up

Class exercises

Talk about the design and implementation logic of payment process

PHP MySQL inserts multiple pieces of data

Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028

Micro service component sentinel console call
随机推荐
PHP returns 500 errors but no error log - PHP return 500 error but no error log
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
数学公式(测试)
PHP MySQL inserts data
Redis core technology and practice - learning notes (11): why not just string
Win32: analyse du fichier dump pour la défaillance du tas
link preload prefetch
Keepalived 设置不抢占资源
[linux]centos 7 reports an error when installing MySQL "no package MySQL server available" no package ZABBIX server MySQL available
How to draw non overlapping bubble chart in MATLAB
A. Berland Poker & 1000 [simple mathematical thinking]
[Tongxin UOS] scanner device management driver installation
Talk about the design and implementation logic of payment process
ArrayList分析3 : 删除元素
Ssl/bio of OpenSSL_ get_ fd
Basic grammar of interview (Part 2)
Research on Swift
PHP MySQL inserts multiple pieces of data
Kotlin的協程:上下文
Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.