当前位置:网站首页>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. .边栏推荐
- Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
- Leetcode 669 pruning binary search tree -- recursive method and iterative method
- Getting started with deops
- How to install PHP on Ubuntu 20.04
- Redis core technology and practice - learning notes (VII) sentinel mechanism
- Valentine's day, send you a little red flower~
- Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
- The third day of writing C language by Yabo people
- A. Berland Poker & 1000 [simple mathematical thinking]
- Leetcode Valentine's Day Special - looking for a single dog
猜你喜欢

Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method

Talk about the design and implementation logic of payment process
![[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)](/img/50/f89092b492d0138304209a72ff05b4.jpg)
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)

Automata and automatic line of non-standard design

聊聊支付流程的設計與實現邏輯

Tensorboard quick start (pytoch uses tensorboard)

Introduction to SolidWorks gear design software tool geartrax

Baiwen.com 7 days Internet of things smart home learning experience punch in the next day

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

聊聊支付流程的设计与实现逻辑
随机推荐
Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
link preload prefetch
PHP MySQL preprocessing statement
STM32实现74HC595控制
Redis core technology and practice - learning notes (11): why not just string
How to install PHP on Ubuntu 20.04
[combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
How to deploy applications on kubernetes cluster
How to purchase Google colab members in China
supervisor监控Gearman任务
PHP MySQL inserts data
Talk about the design and implementation logic of payment process
Deops入门
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
(8) HS corner detection
WebView module manages the application window interface to realize the logical control and management operation of multiple windows (Part 1)
Solve the problem of inaccurate network traffic monitored by ZABBIX with SNMP
基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement