1. 程式人生 > >Golang:根據ascii碼錶將int型 []byte 轉換為 string

Golang:根據ascii碼錶將int型 []byte 轉換為 string



//Golang: 根據ascii碼錶將 []byte 轉換為 string
//以下函式入參為int型切片陣列,函式返回根據ASCII錶轉換後的字串。

import (
    "unsafe"
    "reflect"
)
func BytesToString(b []byte) string {
    bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
    sh := reflect.StringHeader{bh.Data, bh.Len}
    return *(*string)(unsafe.Pointer(&sh))
}