gitee.com/quant1x/gox@v1.21.2/api/string_bytes_below_120.go (about)

     1  //go:build !go1.20
     2  
     3  package api
     4  
     5  import (
     6  	"reflect"
     7  	"unsafe"
     8  )
     9  
    10  // Bytes2String 字节数组转字符串
    11  func Bytes2String(b []byte) string {
    12  	return *(*string)(unsafe.Pointer(&b))
    13  }
    14  
    15  // String2Bytes 字符串转字节数组
    16  func String2Bytes(s string) []byte {
    17  	strh := (*reflect.StringHeader)(unsafe.Pointer(&s))
    18  	var sh reflect.SliceHeader
    19  	sh.Data = strh.Data
    20  	sh.Len = strh.Len
    21  	sh.Cap = strh.Len
    22  	return *(*[]byte)(unsafe.Pointer(&sh))
    23  }